使用BeautifulSoup从Amazon抓取整个类别的产品

2024-09-30 05:19:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我为Amazon编写了一个函数,在给定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感兴趣。有没有一种自动化的方法


Tags: 函数httpscomurlamazondnslinkscript
2条回答

如果您使用google inspector,您将在指向所需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

enter image description here

从这里开始,您需要找到一种方法来概括搜索

您需要一个选择器,该选择器以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)

相关问题 更多 >

    热门问题