containers=page_soup.findAll("div",{"class":"item-container"})
containers[0].div.div当我运行这段代码时,它会输出以下内容。
<div class="item-branding"><a class="item-rating" href="https://www.newegg.com/Yeston-GeForce-GTX-1050-Ti-GTX1050Ti-4G/p/27N-0042-00041?cm_sp=SH-_-946241-_-8-_-2-_-9SIAZUEEV65926-_-graphics-_-graphic-_-4&Item=9SIAZUEEV65926&IsFeedbackTab=true#scrollFullInfo" title="Rating + 5"><i class="rating rating-5"></i><span class="item-rating-num">(<!-- -->3<!-- -->)</span></a></div>
它在这个部门中跳过了一个被称为class=“项目品牌”的部门元素。你怎么能访问它?
发布于 2022-01-22 12:12:38
会发生什么?
有些容器带有赞助产品,结构略有不同,而使用class="item-brand"的标签是<a>。
<div class="item-container">
<div class="item-sponsored-box">
<div class="item-info">
<div class="item-branding">
<a href="https://www.newegg.com/Sabrent/BrandStore/ID-8281" class="item-brand">怎么修?
有不同的战略:
None结果:containers[0].find("a",{"class":"item-brand'})
容器= soup.select('div.item-container:not(:has(.item-sponsored))') containers.div.div.a
注意:新代码中的使用find_all()而不是旧语法findAll()
示例
import requests
from bs4 import BeautifulSoup
url = 'https://www.newegg.com/Video-Cards-Video-Devices/Category/ID-38'
page = requests.get(url)
soup = BeautifulSoup(page.text)
data = []
for item in soup.find_all('div',{'class':'item-container'}):
data.append({
'title':item.find('a', {'class':'item-title'}).text,
'brand':item.find('a', {'class':'item-brand'}).img['title']
})
print(data)输出
[{'title': 'ASRock Phantom Gaming D Radeon RX 6500 XT Video Card RX6500XT PGD 4GO', 'brand': 'ASRock'}, {'title': 'GIGABYTE Eagle Radeon RX 6500 XT Video Card GV-R65XTEAGLE-4GD', 'brand': 'GIGABYTE'}, {'title': 'PowerColor AMD Radeon RX 6500XT ITX Gaming Graphics Card 4GB GDDR6', 'brand': 'PowerColor'}, {'title': 'ASUS TUF Gaming Radeon RX 6900 XT Video Card TUF-RX6900XT-O16G-GAMING', 'brand': 'ASUS'}, {'title': 'ASUS Dual Radeon RX 6500 XT Video Card DUAL-RX6500XT-O4G', 'brand': 'ASUS'}, {'title': 'MSI Mech Radeon RX 6500 XT Video Card RX 6500 XT MECH 2X 4G OC', 'brand': 'MSI'}, {'title': 'SAPPHIRE PULSE Radeon RX 6500 XT Video Card 11314-01-20G', 'brand': 'Sapphire Tech'}, {'title': 'MSI Ventus GeForce RTX 3080 Ti Video Card RTX 3080 Ti VENTUS 3X 12G', 'brand': 'MSI'}, {'title': 'EVGA GeForce RTX 3080 Ti XC3 GAMING Video Card, 12G-P5-3953-KR, 12GB GDDR6X, iCX3 Cooling, ARGB LED, Metal Backplate', 'brand': 'EVGA'}, {'title': 'ASUS ROG Strix NVIDIA GeForce RTX 3080 OC Edition Gaming Graphics Card (PCIe 4.0, 12GB GDDR6X, LHR, HDMI 2.1, DisplayPort 1.4a, Axial-tech Fan Design, 2.9-slot, Super Alloy Power II, GPU Tweak II)', 'brand': 'ASUS'}, {'title': 'ASUS Noctua OC Edition GeForce RTX 3070 Video Card RTX3070-O8G-NOCTUA (LHR)', 'brand': 'ASUS'}, {'title': 'GIGABYTE Eagle GeForce RTX 3080 Ti Video Card GV-N308TEAGLE OC-12GD', 'brand': 'GIGABYTE'}]https://stackoverflow.com/questions/70812259
复制相似问题