我正在尝试让价格显示在:
我尝试了 requests 和 requests-html 但都不起作用......
这是我的代码:
from requests_html import HTMLSession
session = HTMLSession()
from bs4 import BeautifulSoup
response = session.get('https://campervans.jeanlain.com/locations/?city-input=annecy&city-name=ANNECY&departure_date=06%2F01%2F2025&departure_time=11%3A00&return_date=10%2F01%2F2025&return_time=10%3A00')
response.html.render()
soup = BeautifulSoup(response.html.html, 'html.parser')
products = soup.find_all('section', class_='product')
for product in products:
title = product.find('h2', class_='woocommerce-loop-product__title')
if title:
print(title.text)
price_info = product.find('div', class_='content-right')
if price_info:
price = price_info.find('p', class_='price')
print(price)
else:
print("content-right not found")
问题是“content-right”div显示在页面上但没有显示在响应中...它似乎加载了javascript...
如何仅在加载 javascript 时才通过 python 请求显示价格?我不想使用 Selenium...
谢谢:)
我不知道是否有方法来检查 JavaScript 是否已经完成工作,但它允许使用参数
sleep
并且 JavaScript 可能有时间工作。
response.html.render(sleep=3)
它会给你结果。
它还允许发送 JavaScript 代码来检查元素是否存在
response.html.render(script="...")
但我没有任何代码。可能还需要使用
sleep
来实现这一点。
最后一个想法:
selenium
具有运行循环的函数Waits
,并定期检查元素是否已存在。
并且
requests_html
有选项keep_page
request_html.html.render(keep_page=True)
允许通过
html.page
与浏览器页面交互。