python+selenium+chrome批量文件下載并自動(dòng)創(chuàng)建文件夾實(shí)例
實(shí)現(xiàn)效果:通過(guò)url所綁定的關(guān)鍵名創(chuàng)建目錄名,每次訪問(wèn)一個(gè)網(wǎng)頁(yè)url后把文件下載下來(lái)
代碼:
其中 data[i][0]、data[i][1] 是代表 關(guān)鍵詞(文件保存目錄)、網(wǎng)站鏈接(要下載文件的網(wǎng)站)
def getDriverHttp(): for i in range(reCount): # 創(chuàng)建Chrome瀏覽器配置對(duì)象實(shí)例 chromeOptions = webdriver.ChromeOptions() # 設(shè)定下載文件的保存目錄為d盤的tudi目錄, # 如果該目錄不存在,將會(huì)自動(dòng)創(chuàng)建 prefs = {"download.default_directory": "e:\\tudi\\{0}".format(data[i][0]), "profile.default_content_setting_values.automatic_downloads":1} # 將自定義設(shè)置添加到Chrome配置對(duì)象實(shí)例中 chromeOptions.add_experimental_option("prefs", prefs) # 啟動(dòng)帶有自定義設(shè)置的Chrome瀏覽器 # driver = webdriver.Chrome(executable_path="e:\\chromedriver", chrome_options=chromeOptions) driver = webdriver.Chrome(chrome_options=chromeOptions) driver.get(data[i][1]) info2 = re.findall(r'<a href="#" rel="external nofollow" onclick="(.*?)" cssclass="xz_pic">', driver.page_source, re.S) print(len(info2)) for js in info2: driver.execute_script(js) def main(): getDriverHttp()
注意:python 使用selenium下載文件時(shí),chrome會(huì)提示是否下載多個(gè)文件(Download multiple files)
prefs = {"download.default_directory": "e:\\tudi\\{0}".format(data[i][0]), "profile.default_content_setting_values.automatic_downloads":1}
設(shè)置允許多個(gè)文件下載。
補(bǔ)充知識(shí):python項(xiàng)目實(shí)現(xiàn)配置統(tǒng)一管理的操作
一個(gè)比較大的項(xiàng)目總是會(huì)涉及到很多的參數(shù),最好的方法就是在一個(gè)地方統(tǒng)一管理這些參數(shù)。最近看了不少的python項(xiàng)目,總結(jié)了兩種很有意思的配置管理方法。
第一種 基于easydict實(shí)現(xiàn)的配置管理
首先需要安裝numpy、easydict以及yaml:
pip install numpy
pip install easydict
pip install yaml
就可以了。
然后定義配置類config.py:
import numpy as np from easydict import EasyDict as edict import yaml # 創(chuàng)建dict __C = edict() cfg = __C # 定義配置dict __C.dev = edict() __C.dev.name = 'dev-xingoo' __C.dev.age = 20 __C.test = edict() __C.test.name = 'test-xingoo' __C.test.age = 30 # 內(nèi)部方法,實(shí)現(xiàn)yaml配置文件到dict的合并 def _merge_a_into_b(a, b): """Merge config dictionary a into config dictionary b, clobbering the options in b whenever they are also specified in a. """ if type(a) is not edict: return for k, v in a.items(): # a must specify keys that are in b if k not in b: raise KeyError('{} is not a valid config key'.format(k)) # the types must match, too old_type = type(b[k]) if old_type is not type(v): if isinstance(b[k], np.ndarray): v = np.array(v, dtype=b[k].dtype) else: raise ValueError(('Type mismatch ({} vs. {}) ' 'for config key: {}').format(type(b[k]), type(v), k)) # recursively merge dicts if type(v) is edict: try: _merge_a_into_b(a[k], b[k]) except: print(('Error under config key: {}'.format(k))) raise else: b[k] = v # 自動(dòng)加載yaml文件 def cfg_from_file(filename): """Load a config file and merge it into the default options.""" with open(filename, 'r', encoding='utf-8') as f: yaml_cfg = edict(yaml.load(f)) _merge_a_into_b(yaml_cfg, __C)
使用的時(shí)候很簡(jiǎn)單,main.py:
from config import cfg_from_file from config import cfg cfg_from_file('config.yml') print(cfg.dev.name) print(cfg.test.name)
同級(jí)目錄下創(chuàng)建配置文件config.yaml
dev:
name: xingoo-from-yml
輸出:
xingoo-from-yml
test-xingoo
總結(jié)
這樣的好處就是在任何的Python文件中只要from config import cfg就可以使用配置文件。
第二種 基于Class實(shí)現(xiàn)
這種基于普通的python對(duì)象實(shí)現(xiàn)的,創(chuàng)建config2.py:
class Config: def __init__(self): self.name = 'xingoo-config2' self.age = 100
使用的時(shí)候直接創(chuàng)建一個(gè)新的對(duì)象,如何python模塊之間需要引用這個(gè)變量,那么需要把配置對(duì)象傳過(guò)去:
import config2 as config2 cfg2 = config2.Config() print(cfg2.name) print(cfg2.age)
輸出為:
xingoo-config2
100
總結(jié)
第二種方法簡(jiǎn)單粗暴...不過(guò)每次傳遞參數(shù)也是很蛋疼。還是喜歡第一種方式。
以上這篇python+selenium+chrome批量文件下載并自動(dòng)創(chuàng)建文件夾實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python3爬蟲中識(shí)別圖形驗(yàn)證碼的實(shí)例講解
在本篇內(nèi)容里小編給大家分享的是關(guān)于Python3爬蟲中識(shí)別圖形驗(yàn)證碼的實(shí)例講解內(nèi)容,需要的朋友們可以學(xué)習(xí)參考下。2020-07-07使用Python實(shí)現(xiàn)漢諾塔問(wèn)題示例
這篇文章主要介紹了使用Python實(shí)現(xiàn)漢諾塔問(wèn)題示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04Python獲取接口數(shù)據(jù)的實(shí)現(xiàn)示例
本文主要介紹了Python獲取接口數(shù)據(jù)的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-0710個(gè)python爬蟲入門基礎(chǔ)代碼實(shí)例 + 1個(gè)簡(jiǎn)單的python爬蟲完整實(shí)例
這篇文章主要介紹了10個(gè)python爬蟲入門基礎(chǔ)代碼實(shí)例和1個(gè)簡(jiǎn)單的python爬蟲爬蟲貼吧圖片的實(shí)例,需要的朋友可以參考下2020-12-12