【问题标题】:web scraping by BeautifulSoup pythonBeautifulSoup python的网页抓取
【发布时间】:2020-09-14 17:44:19
【问题描述】:

我正在尝试抓取 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 就会显示此错误:

Traceback(最近一次调用最后一次):文件 “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 行,在__getattr__ raise AttributeError(AttributeError: ResultSet 对象没有属性“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

【问题讨论】:

  • 仅供参考,这是刮而不是刮

标签: python web-scraping beautifulsoup


【解决方案1】:

您需要迭代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 方法将返回所有 divc-product-box__content

【讨论】:

    猜你喜欢
    • 2018-04-25
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多