为什么在尝试使用 selenium webdriver 抓取数据时出现此错误(IndentationError:意外缩进)? [重复]

问题描述 投票:0回答:1

我正在尝试开发一个网络爬虫,从 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 selenium
1个回答
-1
投票

Check this image

检查这两个功能,从视觉上看,两者在缩进方面是相同的,但在逻辑上在空格方面有所不同...因此需要使用这些类型的编辑器,可以分别显示空格和制表符空格...

所以再次检查你的代码...这是大多数Python开发人员经常遇到的问题。我找到了解决这个问题的方法。

© www.soinside.com 2019 - 2024. All rights reserved.