簡體   English   中英

嘗試使用 selenium webdriver 抓取數據時,為什么會出現此錯誤(IndentationError:意外縮進)?

[英]Why am I getting this error (IndentationError: unexpected indent) when trying to scrape data using selenium webdriver?

我正在嘗試開發一個從 StockX 收集運動鞋信息的網絡爬蟲。 這是我的代碼。

 
def SneakersInfoScraper():
    # Basic sneaker information

    time.sleep(2)
    driver = webdriver.Chrome(path)

    driver.get("https://stockx.com/nike-air-air-jordan-3-jth")
    Model_name = driver.find_element_by_xpath(
        '//*[@id="product-header"]/div[1]/div/h1'
    ).text
    # Retrieves the name of the Model
    print(Model_name)
    print("=" * 50)

    Color_Sneaker = driver.find_element_by_xpath('//div[@class="detail"][2]/span').text
    # Retrieves the color of the sneaker
    print(Color_Sneaker)
    print("=" * 50)

    Retail_Price = driver.find_element_by_xpath('//div[@class="detail"][3]/span').text
    # Retrieves the release date of the model
    print(Retail_Price)
    print("=" * 50)

    Release_date = driver.find_element_by_xpath('//div[@class="detail"][4]/span').text
    # Retrieves the release date of the model
    print(Release_date)
    print("=" * 50)

    OtherPriceInfo = driver.find_elements_by_xpath('//div[@class="gauges"]')

    for Each_Price_Info in OtherPriceInfo:
        Price_Premium = Each_Price_Info.find_element_by_xpath(
            '//*[@id="root"]/div[1]/div[2]/div[2]/div[8]/div/div/div/div[3]/div[2]/div[2]/div[3]'
        ).text

    print(Price_Premium)
    print("=" * 50)

它像黃油一樣順利運行,直到它遇到 OtherPriceInfo 變量並且我設置了 for 循環。 在輸出中,我可以看到我的格式完全混亂,我不知道為什么。 錯誤:

...     print("=" * 50)
...     OtherPriceInfo = driver.find_elements_by_xpath('//div[@class="gauges"]')
...     for Each_Price_Info in OtherPriceInfo:
...         Price_Premium = Each_Price_Info.find_element_by_xpath(
...             '//*[@id="root            '//*[@id="root     div/d            '//*[@id="root            '//*[@id="ro
  File "<stdin>", line 27
    '//*[@id="root            '//*[@id="root     div/d            '//*[@id="root            '//*[@id="ro
                                 ^
SyntaxError: invalid syntax
>>>     print(Price_Premium)
  File "<stdin>", line 1
    print(Price_Premium)
    ^
IndentationError: unexpected indent
>>>     print("=" * 50)
  File "<stdin>", line 1
    print("=" * 50)
    ^
IndentationError: unexpected indent

任何人都可以建議如何防止這種情況發生? 是python錯誤嗎?

檢查這張圖片

檢查這兩個功能,視覺上兩者在縮進方面是相同的,但在空間方面在邏輯上不同......所以需要使用這些類型的可以分別顯示空格和制表符的編輯器......

所以再次檢查你的代碼......這是大多數python開發人員通常會出現的問題。 我找到了解決這個問題的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM