【发布时间】:2021-04-28 19:24:38
【问题描述】:
我正在尝试从使用 JS 加载呈现 HTML 的网站上抓取产品数据。我使用了 Selenium,具有滚动到页面末尾的功能以及重新加载页面的时间,但仍然只能抓取网站中排名前 8 的产品。
这是我的代码:
url = 'https://www.faire.com/retailer/r_9vkjixqbpq/category/Beauty%20&%20Wellness/subcategory/Bath%20&%20Body?filters=sorting%3Afeatured'
wd.get(url)
last_height = wd.execute_script("return document.documentElement.scrollHeight")
time.sleep(3)
while True:
# Scroll down to bottom
wd.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Wait to load page
time.sleep(20)
# Calculate new scroll height and compare with last scroll height
new_height = wd.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
html = wd.page_source
soup = BS(html, 'lxml')
listings = soup.find('div', {'class': 'MarketplaceProductList__Wrapper-sc-3mfb9g-0 fWFKvm'}).findAll('div', {'class':'MarketplaceProductList__TileWrapper-sc-3mfb9g-2 fQbbTY'})
for item in listings:
product_name = item.find('span', {"class":"FallbackHandler__ContentParentWrapper-sk18il-2 jHwpkw"}).get_text(strip=True)
print(product_name)
如何从页面上的每个产品中提取信息?谢谢!
【问题讨论】:
标签: javascript html selenium web-scraping beautifulsoup