Python无头爬虫下载文件的实现
作者:敲键盘的猫 时间:2023-11-30 03:24:00
有些页面并不能直接用requests获取到内容,会动态执行一些js代码生成内容。这个文章主要是对付那些特殊页面的,比如必须要进行js调用才能下载的情况。
安装chrome
wget [https://dl.google.com/linux/direct/google-chrome-stable\_current\_x86\_64.rpm](https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm)
yum install ./google-chrome-stable\_current\_x86\_64.rpm
yum install mesa-libOSMesa-devel gnu-free-sans-fonts wqy-zenhei-fonts
安装chromedriver
淘宝源(推荐)
wget http://npm.taobao.org/mirrors/chromedriver/2.41/chromedriver_linux64.zip
unzip chromedriver\_linux64.zip
move chromedriver /usr/bin/
chmod +x /usr/bin/chromedriver
感谢这篇博客
上述步骤可以选择适合自己的版本下载,注意:chrome和chrome driver必须是匹配的版本,chrome driver会备注支持的chrome版本号。
实战操作
需要引入的库
from selenium import webdriver
from time import sleep
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import NoSuchElementException
chrome启动设置
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')#解决DevToolsActivePort文件不存在的报错
chrome_options.add_argument('window-size=1920x3000') #指定浏览器分辨率
chrome_options.add_argument('--disable-gpu') #谷歌文档提到需要加上这个属性来规避bug
chrome_options.add_argument('--hide-scrollbars') #隐藏滚动条, 应对一些特殊页面
chrome_options.add_argument('blink-settings=imagesEnabled=false') #不加载图片, 提升速度
chrome_options.add_argument('--headless') #浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败
同样感谢上面的博客
设置额外参数,比如下载不弹窗和默认下载路径
prefs = {'profile.default_content_settings.popups': 0, 'download.default_directory': './filelist'}
chrome_options.add_experimental_option('prefs', prefs)
初始化驱动
cls.driver=webdriver.Chrome(options=chrome_options)
退出驱动
cls.driver.quit()
请求一个url
cls.driver.get(url)
执行指定js代码
cls.driver.execute_script('console.log("helloworld")')
查找指定元素
subtitle = cls.driver.find_element_by_class_name("fubiaoti").text
来源:https://segmentfault.com/a/1190000022227925/
标签:Python,爬虫,下载
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
CentOS 6.X系统下升级Python2.6到Python2.7 的方法
2023-01-05 03:36:18
调试PHP程序的多种方法介绍
2024-06-05 09:48:51
django admin后管定制-显示字段的实例
2023-07-01 11:34:18
![](https://img.aspxhome.com/file/2023/6/97146_0s.jpg)
MySQL索引失效原理
2024-01-19 01:00:50
![](https://img.aspxhome.com/file/2023/1/107421_0s.png)
block 和 inline 的区别是?
2009-12-08 13:00:00
![](https://img.aspxhome.com/file/UploadPic/200912/8/203330321-54s.gif)
Python文件去除注释的方法
2023-09-02 06:56:17
Python实现抓取页面上链接的简单爬虫分享
2021-03-28 23:03:13
![](https://img.aspxhome.com/file/2023/6/135076_0s.jpg)
Python Django基础二之URL路由系统
2021-05-12 11:50:07
![](https://img.aspxhome.com/file/2023/8/93578_0s.png)
ASP.NET MVC4入门教程(六):验证编辑方法和编辑视图
2024-05-13 09:15:36
![](https://img.aspxhome.com/file/2023/9/126199_0s.png)
解决django框架model中外键不落实到数据库问题
2024-01-13 20:36:12
![](https://img.aspxhome.com/file/2023/3/99123_0s.jpg)
php网络安全中命令执行漏洞的产生及本质探究
2023-05-30 05:34:31
Django权限机制实现代码详解
2022-09-18 01:21:51
Python实现动态图解析、合成与倒放
2022-09-22 12:53:23
![](https://img.aspxhome.com/file/2023/3/133843_0s.gif)
python 正则表达式贪婪模式与非贪婪模式原理、用法实例分析
2022-07-08 06:45:02
详解OpenCV图像的概念和基本操作
2021-07-22 02:05:30
![](https://img.aspxhome.com/file/2023/4/78574_0s.png)
Python利用Redis计算经纬度距离案例
2021-03-05 04:51:35
如何安装并在pycharm使用selenium的方法
2022-02-11 07:00:56
![](https://img.aspxhome.com/file/2023/6/133376_0s.png)
ASP操作XML文件的完整实例
2007-09-26 12:05:00
浅谈Python编程中3个常用的数据结构和算法
2022-02-11 20:15:04
Python3列表删除的三种方式实现
2021-03-26 17:55:55
![](https://img.aspxhome.com/file/2023/5/89305_0s.jpg)