如何使用Python从产品目录页面上网上抓取产品?

时间:2020-01-08 21:45:29

标签: python html web-scraping beautifulsoup kaggle

请注意,在网络抓取方面,我是新手。我已经成功地从另一个目录中刮取了产品,但是这次运气不高。

我正在使用Python的beautifulsoup模块从页面https://drop.com/all-communities/drops抓取产品。但是,无法抓住产品卡。我的问题是存在很多嵌套类,并且在每个标记内列出了多个类。我尝试过使用class属性,但是似乎没有任何作用。

HTML太长,因此我不会在此处包括它,但请看一下。

这是我的代码:

def scrapesinglepage(url):
    page = requests.get(url)
    soup = BeautifulSoup(page.content, 'html.parser')
    allproduct = soup.find_all("div", class_ = ['GridgridItemInner1hwGE', 'Link2link1aAsF', 'GridgridItemInner1hwGE'])

for item in all_product:
    product_name = item.find("div", class_ = ['Text__text__PazWx', 'Text__type--headline__2tK0P', 'Text__line_clamp--2__2UPZw']).get_text()
    print(product_name) # prints 'Audiophile' instead of product name
    scrapesinglepage("https://drop.com/all-communities/drops")

我尝试了find_all方法的其他变体,包括

['Grid___c--1__2obI2', 'wdio_card', 'shop_card', 'Card__card__1RLRm', 'Card__background--light-gray__F8hUw', 'Grid__gridItem__2qOsq']
['Grid___c--1__2obI2',  'wdio_card', 'shop_card', 'wdio_drop_card', 'wdio_drop_card--live', 'Card__card__1RLRm', 'Card__background--light-gray__F8hUw', 'Grid__gridItem__2qOsq']

任何帮助将不胜感激,谢谢!

0 个答案:

没有答案