Python使用urllib2模塊抓取HTML頁面資源的實(shí)例分享
更新時間:2016年05月03日 17:58:27 作者:larry
這篇文章主要介紹了Python使用urllib2模塊抓取HTML頁面資源的實(shí)例分享,將要抓取的頁面地址寫在單獨(dú)的規(guī)則列表中方便組織和重復(fù)使用,需要的朋友可以參考下
先把要抓取的網(wǎng)絡(luò)地址列在單獨(dú)的list文件中
http://www.dbjr.com.cn/article/83440.html http://www.dbjr.com.cn/article/83437.html http://www.dbjr.com.cn/article/83430.html http://www.dbjr.com.cn/article/83449.html
然后我們來看程序操作,代碼如下:
#!/usr/bin/python import os import sys import urllib2 import re def Cdown_data(fileurl, fpath, dpath): if not os.path.exists(dpath): os.makedirs(dpath) try: getfile = urllib2.urlopen(fileurl) data = getfile.read() f = open(fpath, 'w') f.write(data) f.close() except: print with open('u1.list') as lines: for line in lines: URI = line.strip() if '?' and '%' in URI: continue elif URI.count('/') == 2: continue elif URI.count('/') > 2: #print URI,URI.count('/') try: dirpath = URI.rpartition('/')[0].split('//')[1] #filepath = URI.split('//')[1].split('/')[1] filepath = URI.split('//')[1] if filepath: print URI,filepath,dirpath Cdown_data(URI, filepath, dirpath) except: print URI,'error'
相關(guān)文章
python-xpath獲取html文檔的部分內(nèi)容
這篇文章主要介紹了python-xpath獲取html文檔的部分內(nèi)容,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Python入門教程(四十三)Python的NumPy數(shù)據(jù)類型
這篇文章主要介紹了Python入門教程(四十二)Python的NumPy數(shù)組裁切,NumPy有一些額外的數(shù)據(jù)類型,并通過一個字符引用數(shù)據(jù)類型,例如 i 代表整數(shù),u 代表無符號整數(shù)等,需要的朋友可以參考下2023-05-05Python列表刪除元素del、pop()和remove()的區(qū)別小結(jié)
這篇文章主要給大家介紹了關(guān)于Python列表刪除元素del、pop()和remove()的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09