溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

用python爬蟲下載鏈接的方法

發(fā)布時間:2020-11-30 09:35:48 來源:億速云 閱讀:586 作者:小新 欄目:編程語言

小編給大家分享一下用python爬蟲下載鏈接的方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

步驟

1、要利用headers拉動請求,模擬成瀏覽器去訪問網(wǎng)站,跳過最簡單的反爬蟲機制。

2、獲取網(wǎng)頁內(nèi)容,保存在一個字符串content中。

3、構造正則表達式,從content中匹配關鍵詞pattern獲取下載鏈接。需要注意的是,網(wǎng)頁中的關鍵詞出現(xiàn)了兩遍(如下圖),所以我們要利用set()函數(shù)清除重復元素。

4、第三步是遍歷set之后的結果,下載鏈接。

5、設置time.sleep(t),無sleep間隔的話,網(wǎng)站認定這種行為是攻擊,所以我們隔一段時間下載一個,反反爬蟲。

具體代碼

import urllib.request# url request
import re            # regular expression
import os            # dirs
import time
'''
url 下載網(wǎng)址
pattern 正則化的匹配關鍵詞
Directory 下載目錄
'''
def BatchDownload(url,pattern,Directory):
    
    # 拉動請求,模擬成瀏覽器去訪問網(wǎng)站->跳過反爬蟲機制
    headers = {'User-Agent', 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36'}
    opener = urllib.request.build_opener()
    opener.addheaders = [headers]
    
    # 獲取網(wǎng)頁內(nèi)容
    content = opener.open(url).read().decode('utf8')
    
    # 構造正則表達式,從content中匹配關鍵詞pattern
    raw_hrefs = re.findall(pattern, content, 0)
    
    # set函數(shù)消除重復元素
    hset = set(raw_hrefs)
         
    # 下載鏈接
    for href in hset:
        # 之所以if else 是為了區(qū)別只有一個鏈接的特別情況
        if(len(hset)>1):
            link = url + href[0]
            filename = os.path.join(Directory, href[0])
            print("正在下載",filename)
            urllib.request.urlretrieve(link, filename)
            print("成功下載!")
        else:
            link = url +href
            filename = os.path.join(Directory, href)
            print("正在下載",filename)
            urllib.request.urlretrieve(link, filename)
            print("成功下載!")
            
        # 無sleep間隔,網(wǎng)站認定這種行為是攻擊,反反爬蟲
        time.sleep(1)
 
#BatchDownload('https://www1.ncdc.noaa.gov/pub/data/swdi/stormevents/csvfiles/',
#              '(Storm-Data-Export-Format.docx)',
#              'E:\stormevents\csvfiles')
        
#BatchDownload('https://www1.ncdc.noaa.gov/pub/data/swdi/stormevents/csvfiles/',
#              '(Storm-Data-Export-Format.pdf)',
#              'E:\stormevents\csvfiles')
        
#BatchDownload('https://www1.ncdc.noaa.gov/pub/data/swdi/stormevents/csvfiles/',
#              '(StormEvents_details-ftp_v1.0_d(\d*)_c(\d*).csv.gz)',
#              'E:\stormevents\csvfiles')
        
#BatchDownload('https://www1.ncdc.noaa.gov/pub/data/swdi/stormevents/csvfiles/',
#              '(StormEvents_fatalities-ftp_v1.0_d(\d*)_c(\d*).csv.gz)',
#              'E:\stormevents\csvfiles')
 
#BatchDownload('https://www1.ncdc.noaa.gov/pub/data/swdi/stormevents/csvfiles/',
#              '(StormEvents_locations-ftp_v1.0_d(\d*)_c(\d*).csv.gz)',
#              'E:\stormevents\csvfiles')
 
#BatchDownload('https://www1.ncdc.noaa.gov/pub/data/swdi/stormevents/csvfiles/legacy/',
#              '(ugc_areas.csv)',
#              'E:\stormevents\csvfiles\legacy')
 
#BatchDownload('https://www1.ncdc.noaa.gov/pub/data/swdi/stormevents/csvfiles/',
#              '(ugc_areas.csv)',
#              'E:\stormevents\csvfiles')

看完了這篇文章,相信你對用python爬蟲下載鏈接的方法有了一定的了解,想了解更多相關知識,歡迎關注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI