将下载的chromedriver.exe放到Google浏览器的安装目录下





如果配置不成功,重启一下电脑试试。

python爬虫 selenium 抓取 今日头条(ajax异步加载)

https://www.cnblogs.com/hellangels333/p/8762112.html
<https://www.cnblogs.com/hellangels333/p/8762112.html>

路径参数:
executable_path=”C:\Program Files
(x86)\Google\Chrome\Application\chromedriver”
from selenium import webdriver from lxml import etree from pyquery import
PyQueryas pq import time driver = webdriver.Chrome(executable_path="C:\Program
Files (x86)\Google\Chrome\Application\chromedriver") driver.maximize_window()
driver.get('https://www.toutiao.com/') driver.implicitly_wait(10)
driver.find_element_by_link_text('科技').click() driver.implicitly_wait(10) for x
in range(3): js = "var q=document.documentElement.scrollTop="+str(x*500)
driver.execute_script(js) time.sleep(2) time.sleep(5) page = driver.page_source
doc = pq(page) doc = etree.HTML(str(doc)) contents = doc.xpath(
'//div[@class="wcommonFeed"]/ul/li') print(contents) for x in contents: title =
x.xpath('div/div[1]/div/div[1]/a/text()') if title: title = title[0] with open(
'toutiao.txt', 'a+', encoding='utf8')as f: f.write(title+'\n') print(title) else
:pass
运行之后,谷歌浏览器会自动打开,并进入今日头条页面,如下:


当我们要进入具体的新闻页面时,需要每一次都打开一个chrome页面,会使得电脑变卡,我们希望不要每次都打开页面,这时,我们可以在每次访问网页的时候
driver.close() 或者使用无界面方法。
selenium无界面chromedriver参考如下:

https://www.cnblogs.com/z-x-y/p/9026226.html
<https://www.cnblogs.com/z-x-y/p/9026226.html>

另一个安装参考博客,未验证过
1. 首先需要下载chromeDriver.
http://chromedriver.storage.googleapis.com/index.html
<http://chromedriver.storage.googleapis.com/index.html>
2. 将下载好的文件解压,将chromedriver.exe拷贝到chrome根目录,一般是在appication目录下

3.可以使用chromeDriver了,如果把chromedriver拷贝到项目根目录就不需要setProperty了,但是我是放到了Chrome根目录下,所以要设置驱动路径。
System.setProperty(“webdriver.chrome.driver”,”C:\Program Files
(x86)\Google\Chrome\Application\chromedriver.exe”);
WebDriver driver=new ChromeDriver(); //初始化浏览器
4.以上三步后,其实就可以直接进入脚本的编写了。

https://www.cnblogs.com/amy-2013/p/4615279.html
<https://www.cnblogs.com/amy-2013/p/4615279.html>

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信