用python爬蟲批量下載pdf的實(shí)現(xiàn)
今天遇到一個(gè)任務(wù),給一個(gè)excel文件,里面有500多個(gè)pdf文件的下載鏈接,需要把這些文件全部下載下來。我知道用python爬蟲可以批量下載,不過之前沒有接觸過。今天下午找了下資料,終于成功搞定,免去了手動(dòng)下載的煩惱。
由于我搭建的python版本是3.5,我學(xué)習(xí)了上面列舉的參考文獻(xiàn)2中的代碼,這里的版本為2.7,有些語法已經(jīng)不適用了。我修正了部分語法,如下:
# coding = UTF-8 # 爬取李東風(fēng)PDF文檔,網(wǎng)址:http://www.math.pku.edu.cn/teachers/lidf/docs/textrick/index.htm import urllib.request import re import os # open the url and read def getHtml(url): page = urllib.request.urlopen(url) html = page.read() page.close() return html # compile the regular expressions and find # all stuff we need def getUrl(html): reg = r'(?:href|HREF)="?((?:http://)?.+?\.pdf)' url_re = re.compile(reg) url_lst = url_re.findall(html.decode('gb2312')) return(url_lst) def getFile(url): file_name = url.split('/')[-1] u = urllib.request.urlopen(url) f = open(file_name, 'wb') block_sz = 8192 while True: buffer = u.read(block_sz) if not buffer: break f.write(buffer) f.close() print ("Sucessful to download" + " " + file_name) root_url = 'http://www.math.pku.edu.cn/teachers/lidf/docs/textrick/' raw_url = 'http://www.math.pku.edu.cn/teachers/lidf/docs/textrick/index.htm' html = getHtml(raw_url) url_lst = getUrl(html) os.mkdir('ldf_download') os.chdir(os.path.join(os.getcwd(), 'ldf_download')) for url in url_lst[:]: url = root_url + url getFile(url)
上面這個(gè)例子是個(gè)很好的模板。當(dāng)然,上面的還不適用于我的情況,我的做法是:先把地址寫到了html文件中,然后對(duì)正則匹配部分做了些修改,我需要匹配的地址都是這樣的,http://pm.zjsti.gov.cn/tempublicfiles/G176200001/G176200001.pdf。改進(jìn)后的代碼如下:
# coding = UTF-8 # 爬取自己編寫的html鏈接中的PDF文檔,網(wǎng)址:file:///E:/ZjuTH/Documents/pythonCode/pythontest.html import urllib.request import re import os # open the url and read def getHtml(url): page = urllib.request.urlopen(url) html = page.read() page.close() return html # compile the regular expressions and find # all stuff we need def getUrl(html): reg = r'([A-Z]\d+)' #匹配了G176200001 url_re = re.compile(reg) url_lst = url_re.findall(html.decode('UTF-8')) #返回匹配的數(shù)組 return(url_lst) def getFile(url): file_name = url.split('/')[-1] u = urllib.request.urlopen(url) f = open(file_name, 'wb') block_sz = 8192 while True: buffer = u.read(block_sz) if not buffer: break f.write(buffer) f.close() print ("Sucessful to download" + " " + file_name) root_url = 'http://pm.zjsti.gov.cn/tempublicfiles/' #下載地址中相同的部分 raw_url = 'file:///E:/ZjuTH/Documents/pythonCode/pythontest.html' html = getHtml(raw_url) url_lst = getUrl(html) os.mkdir('pdf_download') os.chdir(os.path.join(os.getcwd(), 'pdf_download')) for url in url_lst[:]: url = root_url + url+'/'+url+'.pdf' #形成完整的下載地址 getFile(url)
這就輕松搞定啦。
我參考了以下資料,這對(duì)我很有幫助:
1、廖雪峰python教程
2、用Python 爬蟲批量下載PDF文檔
3、用Python 爬蟲爬取貼吧圖片
4、Python爬蟲學(xué)習(xí)系列教程
到此這篇關(guān)于用python爬蟲批量下載pdf的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)python爬蟲批量下載pdf內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)線程池工作模式的案例詳解
這篇文章給大家介紹Python實(shí)現(xiàn)線程池工作模式的相關(guān)知識(shí),本文基于Socket通信方法,自定義數(shù)據(jù)交換協(xié)議,圍繞蘋果樹病蟲害識(shí)別需求,迭代構(gòu)建了客戶機(jī)/服務(wù)器模式的智能桌面App,感興趣的朋友跟隨小編一起看看吧2022-06-06python讀取并顯示圖片的三種方法(opencv、matplotlib、PIL庫)
這篇文章主要給大家介紹了關(guān)于python讀取并顯示圖片的三種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Python RuntimeWarning:invalid value encounter
這篇文章主要介紹了Python RuntimeWarning:invalid value encountered in double_scalars處理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06python 多種日期時(shí)間處理函數(shù)實(shí)例詳解
Python提供了豐富的日期和時(shí)間處理函數(shù),可以幫助你輕松地解析、格式化、計(jì)算和操作日期和時(shí)間,在實(shí)際應(yīng)用中,根據(jù)具體需求選擇合適的函數(shù),可以提高工作效率并簡化代碼,本文給大家介紹python多種日期時(shí)間處理函數(shù)介紹,感興趣的朋友一起看看吧2024-03-03Python實(shí)現(xiàn)提取和去除數(shù)據(jù)中包含關(guān)鍵詞的行
這篇文章主要介紹了Python如何提取數(shù)據(jù)中包含關(guān)鍵詞的行已經(jīng)如何去除數(shù)據(jù)中包含關(guān)鍵詞的行,文中的示例代碼講解詳細(xì),需要的可以參考一下2023-08-08