【发布时间】:2021-01-30 10:17:18
【问题描述】:
我正在做一个网页抓取项目。我正在抓取的网站的 URL 是 https://www.beliani.de/sofas/ledersofa/
我正在抓取此页面上列出的所有产品链接。我尝试使用 Requests-HTML 和 Selenium 获取链接。但我分别得到 57 和 24 个链接。虽然页面上列出了 150 多种产品。 以下是我正在使用的代码块。
使用硒:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from time import sleep
options = Options()
options.add_argument("user-agent = Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36")
#path to crome driver
DRIVER_PATH = 'C:/chromedriver'
driver = webdriver.Chrome(executable_path=DRIVER_PATH, chrome_options=options)
url = 'https://www.beliani.de/sofas/ledersofa/'
driver.get(url)
sleep(20)
links = []
for a in driver.find_elements_by_xpath('//*[@id="offers_div"]/div/div/a'):
print(a)
links.append(a)
print(len(links))
使用 Request-HTML:
from requests_html import HTMLSession
url = 'https://www.beliani.de/sofas/ledersofa/'
s = HTMLSession()
r = s.get(url)
r.html.render(sleep = 20)
products = r.html.xpath('//*[@id="offers_div"]', first = True)
#Getting 57 links using below block:
links = []
for link in products.absolute_links:
print(link)
links.append(link)
print(len(links))
我不知道我做错了哪一步或缺少了什么。
【问题讨论】:
标签: python selenium web-scraping python-requests-html