python - 为什么在尝试使用 selenium webdriver 抓取数据时出现此错误(IndentationError:意外缩进)?
问题描述
我正在尝试开发一个从 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开发人员通常会遇到的问题。我找到了解决这个问题的方法。
推荐阅读
- google-cloud-pubsub - 无法使用部署管理器创建主题
- typescript - 函数式 TypeScript 和泛型
- android - Android Auto 和 TensorFlow?
- javascript - 为什么我的 Wikipedia API 搜索不起作用?
- c++ - 是否可以将 const unique_ptr 引用(派生类)转换为 shared_ptr(基类)
- android - READ_SMS 和 SEND_SMS 权限替代
- java - Firebase order by child of a child to a query for FirebaseRecyclerAdapter
- scala - intellij 的 scala 插件无法识别 build.sbt 中的符号
- spring - org.hsqldb.HsqlException:用户缺少权限或找不到对象:
- byte - WebAuthn - “凭证公钥”的字节长度