【发布时间】:2023-04-05 17:50:02
【问题描述】:
我是一个新手,试图使用 bs4抓取这个网站,方法是从指定的 div 收集 href,然后通过 href 浏览产品页面并收集数据,但我一直在收集的href。 如果有人帮助我解决这个问题,我会非常高兴:
import urllib.request
from bs4 import BeautifulSoup
urlpage = 'https://www.digikala.com/search/category-tire/'
print(urlpage)
# scrape the webpage using beautifulsoup
# query the website and return the html to the variable 'page'
page = urllib.request.urlopen(urlpage)
# parse the html using beautiful soup and store in variable 'soup'
soup = BeautifulSoup(page, 'html.parser')
# find product items
results = soup.find_all('div', attrs={'class': 'c-product-box__title'})
print('BeautifulSoup - Number of results', len(results))
这是第一个结果,虽然当你打印结果时它会附带 36 个 div,我只是复制了第一个,我尽力不问并找到答案,但我什至没有接近,所以我很抱歉,如果它这么简单。
<div class="c-product-box__title"><a href="/product/dkp-539563/لاستیک-خودرو-میشلن-مدل-primacy-3-سایز-20555r16-دو-حلقه" target="_blank">لاستیک خودرو میشلن مدل Primacy 3 سایز 205/55R16 - دو حلقه</a></div>
【问题讨论】:
标签: python web-scraping beautifulsoup