我是bs4的新手,我正尝试抓取一些有关亚马逊产品的信息以供大学分配,特别是我正尝试从html页面提取产品类别。我试图以这种方式提取它,但得到一个空数组。
我需要提取:杂货和美食,糖果和巧克力,软糖和软糖,欧亚甘草
这是我要抓取的网页部分,但我不知道如何访问它:
<div id="wayfinding-breadcrumbs_container" class="a-section a-spacing-none a-padding-medium breadcrumb-fst-exp-1 fst-breadcrumb-feature">
<ul class="a-unordered-list a-horizontal a-size-small">
<li><span class="a-list-item">
<a class="a-link-normal" href="/grocery-breakfast-foods-snacks-organic/b/ref=dp_bc_aui_T1_1?ie=UTF8&node=16310101">
Grocery & Gourmet Food
</a>
</span></li>
<li><span class="a-list-item">
<a class="a-link-normal" href="/Candy-Chocolate/b/ref=dp_bc_aui_T1_2?ie=UTF8&node=16322461">
Candy & Chocolate
</a>
</span></li>
<li><span class="a-list-item">
<a class="a-link-normal" href="/b/ref=dp_bc_aui_T1_3?ie=UTF8&node=17369013011">
Jelly Beans & Gummy Candy
</a>
</span></li>
<li><span class="a-list-item">
<a class="a-link-normal" href="/Licorice-Candy/b/ref=dp_bc_aui_T1_4?ie=UTF8&node=16322521">
Licorice
</a>
</span></li>
</ul>
</div>
import requests
from bs4 import BeautifulSoup
url = "https://www.amazon.com/dp/" + 'B001GVISJM'
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.content, "html.parser")
for divtag in soup.find_all("div", attr={"id" : "wayfinding-breadcrumbs_container"}):
print(divtag)
答案 0 :(得分:1)
您可以按照以下方式进行操作。对于id的findind,您可以将其作为函数参数传递,而不是在attrs
内部传递。
from bs4 import BeautifulSoup
t = '''
<div id="wayfinding-breadcrumbs_container" class="a-section a-spacing-none a-padding-medium breadcrumb-fst-exp-1 fst-breadcrumb-feature">
<ul class="a-unordered-list a-horizontal a-size-small">
<li><span class="a-list-item">
<a class="a-link-normal" href="/grocery-breakfast-foods-snacks-organic/b/ref=dp_bc_aui_T1_1?ie=UTF8&node=16310101">
Grocery & Gourmet Food
</a>
</span></li>
<li><span class="a-list-item">
<a class="a-link-normal" href="/Candy-Chocolate/b/ref=dp_bc_aui_T1_2?ie=UTF8&node=16322461">
Candy & Chocolate
</a>
</span></li>
<li><span class="a-list-item">
<a class="a-link-normal" href="/b/ref=dp_bc_aui_T1_3?ie=UTF8&node=17369013011">
Jelly Beans & Gummy Candy
</a>
</span></li>
<li><span class="a-list-item">
<a class="a-link-normal" href="/Licorice-Candy/b/ref=dp_bc_aui_T1_4?ie=UTF8&node=16322521">
Licorice
</a>
</span></li>
</ul>
</div>
'''
soup = BeautifulSoup(t, 'html.parser')
for divtag in soup.find_all(id="wayfinding-breadcrumbs_container"):
for d in divtag.find_all(attrs={'class': 'a-link-normal'}):
print(d.get_text().strip())
输出:
Grocery & Gourmet Food
Candy & Chocolate
Jelly Beans & Gummy Candy
Licorice
答案 1 :(得分:0)
快速选择器(将面包屑的ID组合在一起,然后为链接输入a
的选择器。
categories = [item['href'] for item in soup.select('#wayfinding-breadcrumbs_feature_div a')]
答案 2 :(得分:0)
尝试一下:
for i in soup.find('div', id='wayfinding-breadcrumbs_container').find('ul').find_all('li', class_=None):
text = i.find('a').text.strip()
print(text)
或
for i in soup.select('#wayfinding-breadcrumbs_container a'):
text = i.text.strip()
print(text)
实际的亚马逊页面有另一个div,您无需考虑。结构是:
<div>
<div> <--- missing in your post
<ul>
<li>
...
附带说明:众所周知,亚马逊对刮刮乐非常挑剔。如果不使用计时器,代理,Cookie和冗余循环,则您的抓取工具会非常不一致。