我似乎看不出少了什么。为什么回应没有打印ASIN
import requests
import re
urls = [
'https://www.amazon.com/s?k=xbox+game&ref=nb_sb_noss_2',
'https://www.amazon.com/s?k=ps4+game&ref=nb_sb_noss_2'
]
for url in urls:
content = requests.get(url).content
decoded_content = content.decode()
asins = set(re.findall(r'/[^/]+/dp/([^"]+)', decoded_content))
print(asins)
回溯
set()
set()
[Finished in 0.735s]
正则表达式不应用于解析HTML。对于像这样的问题,不建议将正则表达式用于HTML。很难编写足够复杂的正则表达式来从每个
<div>
获取数据asin值。{a3}将使这项任务更容易。但如果必须使用regex,则此代码将返回body标记内的所有内容:另外,打印
decoded_content
并阅读HTML。您可能没有收到在web浏览器中看到的相同网站。使用你的代码,我只是从Amazon上收到一条错误消息,或者通过一个小测试来确定我是否是一个机器人。如果你的请求没有真正的标题,像亚马逊这样的大型网站将不会返回你想要的页面。他们试图阻止人们刮掉他们的网站下面是一些使用BeautifulSoup库的代码。您需要首先安装库
pip3 install bs4
如果您正在从文件中读取html,则:
相关问题 更多 >
编程相关推荐