Python 如何使用延迟加载下载页面?

Python 如何使用延迟加载下载页面?,python,web-scraping,urllib,Python,Web Scraping,Urllib,我需要下载整个页面并对其进行解析,但它会使用帮助JavaScript创建一些元素。当我尝试使用helpurllib执行此操作时,我会收到一个没有使用JavaScript元素的html页面。我怎样才能解决这个问题 将urlib.request导入为urlib page=urlib.urlopen('https://www.example.com') soup=BeautifulSoup(第页“html5lib”) ... 尝试: colordiv = soup.select("div.pswp_

我需要下载整个页面并对其进行解析,但它会使用帮助JavaScript创建一些元素。当我尝试使用helpurllib执行此操作时,我会收到一个没有使用JavaScript元素的html页面。我怎样才能解决这个问题

将urlib.request导入为urlib
page=urlib.urlopen('https://www.example.com')
soup=BeautifulSoup(第页“html5lib”)
...
尝试:

colordiv = soup.select("div.pswp__item:nth-child(1) > div:nth-child(1) > img:nth-child(1)'")[0]
与:


您可以使用开发工具查找用于更新颜色值的请求

import requests

r = requests.get('https://www.electrictobacconist.com/ajax/get_product_options/5831').json()
colours = [item['value'] for item in r['attributes'][0]['values']]
print(colours)


即使页面是使用JavaScript呈现的,数据也是通过后台的ajax响应接收的。你所要做的就是提出这个要求

import requests
import re
url='https://www.electrictobacconist.com/smok-nord-p5831'
#get 5831
product_id=re.findall(r'\d+', url)[-1]
r=requests.get("https://www.electrictobacconist.com/ajax/get_product_options/{}".format(product_id))
print([x['value'] for x in r.json()['attributes'][0]['values']])
输出:

['Black/Blue', 'Black/White', 'Bottle Green', 'Full Black', 'Prism Gold', 'Prism Rainbow', 'Red', 'Resin Rainbow', 'Yellow/Purple', 'Blue/Brown', 'Red/Yellow', 'Red/Green', 'Black/White Resin']

你能把这个网址包括进去并解释清楚你想要什么吗?选择颜色
colordiv=soup。选择(“div.pswp\uu项目:第n个孩子(1)>div:第n个孩子(1)>img:第n个孩子(1)”)[0]
使用硒。。。
['Black/Blue', 'Black/White', 'Bottle Green', 'Full Black', 'Prism Gold', 'Prism Rainbow', 'Red', 'Resin Rainbow', 'Yellow/Purple', 'Blue/Brown', 'Red/Yellow', 'Red/Green', 'Black/White Resin']