我已经为亚马逊编写了一个函数,给出了一个URL,它为我提供了产品的标题、价格和评级。如果我给它一个字符串格式的URL,效果会很好。我想使用这个功能,比如说它叫做AmazonCrawler,以便从网站上抓取一个完整的产品类别,而不仅仅是一个产品。我该怎么做呢?
编辑:
这里有一个我想要抓取的示例页面:Amazon TV Category。如果我查看页面源代码,我会发现:
<script type='text/javascript'>var ue_t0=ue_t0||+new Date();</script>
<!-- sp:feature:cs-optimization -->
<meta http-equiv='x-dns-prefetch-control' content='on'>
<link rel="dns-prefetch" href="https://images-eu.ssl-images-amazon.com">
<link rel="dns-prefetch" href="https://m.media-amazon.com">
<link rel="dns-prefetch" href="https://completion.amazon.com">
<script type='text/javascript'>
window.ue_ihb = (window.ue_ihb || window.ueinit || 0) + 1;
if (window.ue_ihb === 1) {我对一种在亚马逊网站上找到所有智能电视的URL的方法感兴趣。有没有一种自动化的方式来做这件事?
发布于 2021-03-31 18:15:16
您需要一个选择器,它以src以.jpg结尾的所有img为目标,但也需要排除一些其他较早的匹配。使用:not和前面的.a-row可以做到这一点。最后,您需要使用set来清理唯一项。
import requests
from bs4 import BeautifulSoup as bs
from pprint import pprint
r = requests.get('https://www.amazon.es/b/ref=sv_ap_arrow_ce_4_1_1_1?node=934359031', headers = {'User-Agent':'Mozilla/5.0'})
soup = bs(r.content, 'lxml')
images = set(i['src'] for i in soup.select('.a-row img[src$=jpg]:not(.bxc-grid__row:nth-child(1) img[src$=jpg])'))
pprint(images)发布于 2021-03-31 17:00:43
如果你使用谷歌检查器,你会在图片上找到指向你想要的URL的href。例如,您找到的第一个Samsum TV的href位于以下Xpath:
/html/body/div[1]/div[2]/div[2]/div[1]/div[3]/div[2]/div[2]/ul/li[1]/span/div/a

从这里开始,您需要找到一种方法来推广搜索
https://stackoverflow.com/questions/66883978
复制相似问题