Python無頭爬蟲下載文件的實(shí)現(xiàn)
有些頁面并不能直接用requests獲取到內(nèi)容,會動態(tài)執(zhí)行一些js代碼生成內(nèi)容。這個(gè)文章主要是對付那些特殊頁面的,比如必須要進(jìn)行js調(diào)用才能下載的情況。
安裝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版本號。
實(shí)戰(zhàn)操作
需要引入的庫
from selenium import webdriver from time import sleep from selenium.webdriver.chrome.options import Options from selenium.common.exceptions import NoSuchElementException
chrome啟動設(shè)置
chrome_options = Options() chrome_options.add_argument('--no-sandbox')#解決DevToolsActivePort文件不存在的報(bào)錯(cuò) chrome_options.add_argument('window-size=1920x3000') #指定瀏覽器分辨率 chrome_options.add_argument('--disable-gpu') #谷歌文檔提到需要加上這個(gè)屬性來規(guī)避bug chrome_options.add_argument('--hide-scrollbars') #隱藏滾動條, 應(yīng)對一些特殊頁面 chrome_options.add_argument('blink-settings=imagesEnabled=false') #不加載圖片, 提升速度 chrome_options.add_argument('--headless') #瀏覽器不提供可視化頁面. linux下如果系統(tǒng)不支持可視化不加這條會啟動失敗
同樣感謝上面的博客
設(shè)置額外參數(shù),比如下載不彈窗和默認(rèn)下載路徑
prefs = {'profile.default_content_settings.popups': 0, 'download.default_directory': './filelist'} chrome_options.add_experimental_option('prefs', prefs)
初始化驅(qū)動
cls.driver=webdriver.Chrome(options=chrome_options)
退出驅(qū)動
cls.driver.quit()
請求一個(gè)url
cls.driver.get(url)
執(zhí)行指定js代碼
cls.driver.execute_script('console.log("helloworld")')
查找指定元素
subtitle = cls.driver.find_element_by_class_name("fubiaoti").text
到此這篇關(guān)于Python無頭爬蟲下載文件的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Python無頭爬蟲下載文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python 實(shí)現(xiàn)將大圖切片成小圖,將小圖組合成大圖的例子
這篇文章主要介紹了Python 實(shí)現(xiàn)將大圖切片成小圖,將小圖組合成大圖的例子,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Python多維/嵌套字典數(shù)據(jù)無限遍歷的實(shí)現(xiàn)
下面小編就為大家?guī)硪黄狿ython多維/嵌套字典數(shù)據(jù)無限遍歷的實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11Python+Redis從零打造分布式鎖實(shí)戰(zhàn)示例
Redis作為一款高性能的內(nèi)存鍵值數(shù)據(jù)庫,憑借其支持原子操作、高并發(fā)和數(shù)據(jù)持久化等特性,非常適合用來實(shí)現(xiàn)分布式鎖,本文將詳細(xì)探討如何使用Python結(jié)合Redis從簡單到復(fù)雜地實(shí)現(xiàn)分布式鎖,并提供相應(yīng)的示例代碼2024-01-01利用python微信庫itchat實(shí)現(xiàn)微信自動回復(fù)功能
最近發(fā)現(xiàn)了一個(gè)特別好玩的Python 微信庫itchat,可以實(shí)現(xiàn)自動回復(fù)等多種功能,下面這篇文章主要給大家介紹了利用python微信庫itchat實(shí)現(xiàn)微信自動回復(fù)功能的相關(guān)資料,需要的朋友可以參考學(xué)習(xí),下面來一起看看吧。2017-05-05pandas如何將dataframe中的NaN替換成None
這篇文章主要介紹了pandas如何將dataframe中的NaN替換成None問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08