python - BeautifulSoup python的网页抓取
问题描述
我正在尝试通过 BeautifulSoup抓取在线商店的产品,但我有一个问题,我只能抓取一种产品!,但我想刮掉所有产品。
import requests
from bs4 import BeautifulSoup
res = requests.get('https://www.digikala.com/search/category-wearable-gadget/')
soup = BeautifulSoup(res.text, 'html.parser')
result = soup.find_all('div', class_='c-product-box__content')
first_watch = result[0]
first_name = first_watch.a.text
first_rate = first_watch.find(class_='c-product-box__rate-comparision--rate-people')
first_price = first_watch.find(class_='c-price__value-wrapper')
first_rate = first_rate.text
first_price = first_price.text
一旦我使用first_watch = result
并使用find_all
显示此错误:
回溯(最后一次调用):文件“C:/Users/Oogway/PycharmProjects/web_scraping1/web_s.py”,第 8 行,在 first_name = first_watch.a.text 文件“C:\Users\Oogway.virtualenvs\web_scraping1\ lib\site-packages\bs4\element.py",第 2160 行,in__getattr__ raise AttributeError( AttributeError: ResultSet object has no attribute 'a'。您可能将元素列表视为单个元素。您调用 find_all( ) 当你打算调用 find() 时?
import requests
from bs4 import BeautifulSoup
res = requests.get('https://www.digikala.com/search/category-wearable-gadget/')
soup = BeautifulSoup(res.text, 'html.parser')
result = soup.find_all('div', class_='c-product-box__content')
first_watch = result
first_name = first_watch.a.text
first_rate = first_watch.find_all(class_='c-product-box__rate-comparision--rate-people')
first_price = first_watch.find_all(class_='c-price__value-wrapper')
first_rate = first_rate.text
first_price = first_price.text
解决方案
您需要迭代result
对象。
import requests
from bs4 import BeautifulSoup
res = requests.get('https://www.digikala.com/search/category-wearable-gadget/')
soup = BeautifulSoup(res.text, 'html.parser')
result = soup.find_all('div', class_='c-product-box__content')
for itm in result:
print(itm.a.text)
find_all
方法将返回所有div
带类c-product-box__content
。
推荐阅读
- typo3 - 避免在中间表中存储数据时的 TYPO3 后端工作流程
- html - 使用 Bootstrap 在导航栏上方添加顶栏
- javascript - 如何等待用户在 J-Query 文本字段中按下回车键?
- android - 打开带有文件路径android 11的相机意图不起作用
- python - pywinauto 后端“uia”未注册!问题
- python - 如何根据条件在熊猫中创建另一列?
- bash - 在单独的目录中为每次运行编写一个配置文件,然后启动 mpirun
- javascript - 为什么在安卓浏览器上无法播放音频?
- ios - 如何在 SwiftUI 中使用 UIAccessibility.post(notification: .layoutChanged, argument: nil) 将焦点移动到特定视图
- java - Java 排序列表的最佳方式,而不是使用多个循环