模拟浏览器功能,自动执行网页中的js代码,实现动态加载
操作谷歌浏览器驱动下载地址
http://npm.taobao.org/mirrors/chromedriver/下载完成解压
安装selenium
pip install selenium==3.141.0
from selenium import webdriver
path = '谷歌浏览器驱动文件路径'
browser = webdriver.Chrome(path)
url = '要访问的网站地址'
browser.get(url)
# page_source 获取网页源码
content = browser.page_source
完整代码
# 1.导入selenium
from selenium import webdriver
# 2.创建浏览器操作对象
path = 'files/chromedriver.exe'
browser = webdriver.Chrome(path)
# 3.访问网址
url = 'https://www.jd.com/'
browser.get(url)
# page_source 获取网页源码
content = browser.page_source
print(content)