【发布时间】:2021-05-19 13:37:26
【问题描述】:
我正在开发 python 程序,它在获得 steam 包的 ID 后 - 它返回 当前价格。
程序正在使用 requests 和 lxml。
最终价格有两种路径:
- /html/body/div[1]/div[7]/div[4]/div[1]/div[2]/div/div[2]/div[10]/div[3]/ div
- //*[@id="game_area_purchase"]/div/div/div/div[1]/div/div/div[2]
使用示例:https://store.steampowered.com/bundle/16140
这是一个代码:
import requests
import lxml.html
#example URL for steam bundle
URL = "https://store.steampowered.com/bundle/16140"
html = requests.get(URL)
doc = lxml.html.fromstring(html.content)
#xpath to price location
price = doc.xpath('/html/body/div[1]/div[7]/div[4]/div[1]/div[2]/div/div[2]/div[10]/div[3]/div/text()')
print(price)
程序返回这个:
[]
或者这个
Traceback (most recent call last):
File <path-to-program>, line 9, in <module>
price = doc.xpath('/html/body/div[1]/div[7]/div[4]/div[1]/div[2]/div/div[2]/div[10]/div[3]/div/text()')[0]
IndexError: list index out of range
这两个选项都有错误。 我该怎么做才能解决它?
【问题讨论】:
-
当您请求页面
requests时,返回的结果并非您所期望的 - 它返回带有色情内容/裸露警告的页面。您可以尝试创建requests.Session以在单个会话中发送多个请求。请注意,您应该发送带有出生日期数据的 POST 请求。另请注意,您的方法应该适用于不需要年龄验证的其他页面
标签: python xpath python-requests lxml steam