首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >网络抓取Python美丽汤4‘文章’

网络抓取Python美丽汤4‘文章’
EN

Stack Overflow用户
提问于 2020-07-09 19:52:42
回答 1查看 86关注 0票数 0

我正在尝试用python做一个网络爬行器,使用web驱动程序和漂亮的SOUP4。我在decathlon.fr上尝试了代码,问题是包含产品的类是一个“文章”类。当我启动这个程序时,它不需要任何东西,因为我需要这个类是一个“div”类或者“a”类。

代码语言:javascript
复制
from bs4 import BeautifulSoup
driver = webdriver.Chrome("chromedriver.exe")

products=[] #List to store name of the product
prices=[] #List to store price of the product
ratings=[] #List to store rating of the product

driver.get("https://www.decathlon.fr/search?Ntt=t+shirt+anti+uv+b%C3%A9b%C3%A9")

content = driver.page_source
soup = BeautifulSoup(content)
for a in soup.findAll('article', attrs={'class':'dkt-product.js-product-slider-init.product-printed'}):
    name=a.find('a', attrs={'class':'dkt-product__title__wrapper'})
    price=a.find('div', attrs={'class':'dkt-product__price'})
    #rating=a.find('div', attrs={'class':'hGSR34 _2beYZw'})
    rating=a.find('span', attrs={'itemprop':'name'})
    products.append(name)
    prices.append(price)
    ratings.append(rating)


print(products)
print(prices)
print(ratings)
EN

回答 1

Stack Overflow用户

发布于 2020-07-09 20:16:35

您需要在代码中进行简单的更改:

代码语言:javascript
复制
soup = BeautifulSoup(content, 'lxml') # here add 'lxml'
for a in soup.findAll('article', attrs={'class':'dkt-product js-product-slider-init product-printed'}): # here, removed dots
    name= a.find('a', attrs={'class':'dkt-product__title__wrapper'})
    price=a.find('div', attrs={'class':'dkt-price__cartridge'}) # here
    #rating=a.find('div', attrs={'class':'hGSR34 _2beYZw'})
    rating=a.find('span', attrs={'itemprop':'ratingValue'}) # here
    products.append(name)
    prices.append(price)
    ratings.append(rating)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62814238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档