如何用Python无头浏览器实现文件下载?

2026-05-26 23:501阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计508个文字,预计阅读时间需要3分钟。

如何用Python无头浏览器实现文件下载?

某些页面无法直接使用requests获取内容,会动态执行一些js代码生成内容。本文主要针对那些需要js调用的特殊页面,例如必须执行js才能下载的情况。安装Chrome和wget:[https://dl.google.com](https://dl.google.com)。

有些页面并不能直接用requests获取到内容,会动态执行一些js代码生成内容。这个文章主要是对付那些特殊页面的,比如必须要进行js调用才能下载的情况。

安装chrome

wget [dl.google.com/linux/direct/google-chrome-stable\_current\_x86\_64.rpm](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 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代码

如何用Python无头浏览器实现文件下载?

cls.driver.execute_script('console.log("helloworld")')

查找指定元素

subtitle = cls.driver.find_element_by_class_name("fubiaoti").text

到此这篇关于Python无头爬虫下载文件的实现的文章就介绍到这了,更多相关Python无头爬虫下载文件内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

本文共计508个文字,预计阅读时间需要3分钟。

如何用Python无头浏览器实现文件下载?

某些页面无法直接使用requests获取内容,会动态执行一些js代码生成内容。本文主要针对那些需要js调用的特殊页面,例如必须执行js才能下载的情况。安装Chrome和wget:[https://dl.google.com](https://dl.google.com)。

有些页面并不能直接用requests获取到内容,会动态执行一些js代码生成内容。这个文章主要是对付那些特殊页面的,比如必须要进行js调用才能下载的情况。

安装chrome

wget [dl.google.com/linux/direct/google-chrome-stable\_current\_x86\_64.rpm](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 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代码

如何用Python无头浏览器实现文件下载?

cls.driver.execute_script('console.log("helloworld")')

查找指定元素

subtitle = cls.driver.find_element_by_class_name("fubiaoti").text

到此这篇关于Python无头爬虫下载文件的实现的文章就介绍到这了,更多相关Python无头爬虫下载文件内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!