由美丽汤蟒蛇进行网页抓取



我正在尝试通过美丽汤抓取在线商店的产品,但我有一个问题,我只能抓取一种产品! ,但我想刮掉所有产品。

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__引发属性错误( 属性错误: 结果集对象没有属性"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方法将返回类c-product-box__content的所有div

最新更新