【发布时间】:2021-04-22 08:15:01
【问题描述】:
所以我有这段代码可以抓取 javascript 内容:
from requests_html import HTMLSession
#create the session
session = HTMLSession()
#define our URL
url = 'https://partalert.net/product.js?asin=B08L8LG4M3&price=%E2%82%AC702.07&smid=A3JWKAKR8XB7XF&tag=partalertde-21×tamp=16%3A33+UTC+%2821.4.2021%29&title=ASUS+DUAL+NVIDIA+GeForce+RTX+3070+OC+Edition+Gaming+Grafikkarte+%28PCIe+4.0%2C+8+GB+GDDR6+Speicher%2C+HDMI+2.1%2C+DisplayPort+1.4a%2C+Axial-tech+L%C3%BCfterdesign%2C+Dual+BIOS%2C+Schutzr%C3%BCckwand%2C+GPU+Tweak+II%29&tld=.de'
#use the session to get the data
r = session.get(url)
#Render the page, up the number on scrolldown to page down multiple times on a page
r.html.render(sleep=0, keep_page=True, scrolldown=0)
#take the rendered html and find the element that we are interested in
links = r.html.find('#href')
#loop through those elements extracting the text and link
for item in links:
link = {
'link': item.absolute_links
}
print(link)
但是它需要 2-3 秒,这对我来说太长了。有没有办法加快速度?
【问题讨论】:
-
如果您查看该 URL 中的脚本的作用,它只会解析您传入的查询字符串中的数据,然后根据该脚本生成一个 Amazon URL。您应该自己解析查询字符串(参见
urllib.parse),然后形成链接。
标签: python python-requests request python-requests-html