gpt4 book ai didi

python - 如何使用python提取亚马逊产品链接

转载 作者:太空宇宙 更新时间:2023-11-03 16:01:02 25 4
gpt4 key购买 nike

我是 Python 初学者,我只想删除亚马逊页面上的产品链接。例如,我想废弃此页面 http://www.amazon.com/s/ref=sr_in_-2_p_4_18?me=A3MZ96G5C78IVQ&fst=as%3Aoff&rh=p_4%3AFunKo&ie=UTF8&qid=1477811368我在 python 中使用这段代码

from bs4 import BeautifulSoup
import requests
url = "http://www.amazon.com/s/ref=sr_in_-2_p_4_18?me=A3MZ96G5C78IVQ&fst=as%3Aoff&rh=p_4%3AFunKo&ie=UTF8&qid=1477811368"
r = requests.get(url)
soup = BeautifulSoup(r.content, "lxml")

file = open("parseddata.txt", "wb")

links = soup.find_all('a', {'class': 'a-link-normal s-access-detail-page a-text-normal'})

for link in links:
print(link.get('href'))
file.write(href + '\n')
file.close()

我只想将产品标题链接作为输出。谁能告诉我哪里做错了。

最佳答案

用户代理添加到请求 header 以假装您不是机器人。

from bs4 import BeautifulSoup
import requests
url = "http://www.amazon.com/s/ref=sr_in_-2_p_4_18?me=A3MZ96G5C78IVQ&fst=as%3Aoff&rh=p_4%3AFunKo&ie=UTF8&qid=1477811368"

# add header
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36'
}
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.content, "lxml")

file = open(r"parseddata.txt", "w")

links = soup.find_all('a', {'class': 'a-link-normal s-access-detail-page a-text-normal'})

for link in links:
print(link.get('href'))
file.write(link.get('href')+ '\n')
file.close()

结果

https://www.amazon.com/Funko-POP-Marvel-Dancing-Bobble/dp/B00N1EJXUU/ref=sr_1_1/160-5408618-6684940?m=A3MZ96G5C78IVQ&s=merchant-items&ie=UTF8&qid=1477822032&sr=1-1&refinements=p_4%3AFunKo
https://www.amazon.com/Funko-POP-Movies-Potter-Action/dp/B019JIA4IQ/ref=sr_1_2/160-5408618-6684940?m=A3MZ96G5C78IVQ&s=merchant-items&ie=UTF8&qid=1477822032&sr=1-2&refinements=p_4%3AFunKo
https://www.amazon.com/FunKo-2390-Funko-Darth-Maul/dp/B005F1QBMK/ref=sr_1_3/160-5408618-6684940?m=A3MZ96G5C78IVQ&s=merchant-items&ie=UTF8&qid=1477822032&sr=1-3&refinements=p_4%3AFunKo
........

关于python - 如何使用python提取亚马逊产品链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40327828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com