window10,pycharm
1. 查看电脑上Chrome浏览器版本
1.1 在Chrome浏览器地址栏中输入 chrome://version/
2. 下载 chromedriver.exe
2.1 下载地址 http://chromedriver.storage.googleapis.com/index.html
2.2 找到和自己安装的chrome对应的版本
下载后解压,得到一个 chromedriver.exe 文件
3. 在 pycharm 中创建 python 项目
3.1 新建一个普通目录用于放置 chromedriver.exe
3.2 新建一个.py文件
pre_dir = r'D:/temp/scrapy/'
driver = webdriver.Chrome('./driver/chromedriver.exe')
# 设置超时时间 10s
driver.set_page_load_timeout(10)
url_str = f"https://www.baidu.com"
driver.get(url_str)
page_source = driver.page_source
file_path = pre_dir + str(i)+'.html'
with open(file_path,'w',encoding='utf-8') as fw:
fw.write(page_source)
driver.close()
driver.quit()
至此,使用 chromedriver.exe 下载网页的功能完毕