为什么在尝试使用selenium webdriver刮取数据时会出现此错误(缩进错误:意外缩进)?

2024-10-06 07:16:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图开发一个webscraper,从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错误吗


Tags: thetextdividbydriverrootelement
1条回答
网友
1楼 · 发布于 2024-10-06 07:16:23

Check this image

检查两个函数,从视觉上看,它们在缩进方面是相同的,但在逻辑上在空间方面是不同的。。。所以需要使用这些类型的编辑器,它们可以分别显示空格和制表符空格

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

相关问题 更多 >