Python使用urllib2模塊抓取HTML頁面資源的實例分享
更新時間:2016年05月03日 17:58:27 作者:larry
這篇文章主要介紹了Python使用urllib2模塊抓取HTML頁面資源的實例分享,將要抓取的頁面地址寫在單獨的規(guī)則列表中方便組織和重復使用,需要的朋友可以參考下
先把要抓取的網絡地址列在單獨的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'
相關文章
Python入門教程(四十三)Python的NumPy數據類型
這篇文章主要介紹了Python入門教程(四十二)Python的NumPy數組裁切,NumPy有一些額外的數據類型,并通過一個字符引用數據類型,例如 i 代表整數,u 代表無符號整數等,需要的朋友可以參考下2023-05-05Python列表刪除元素del、pop()和remove()的區(qū)別小結
這篇文章主要給大家介紹了關于Python列表刪除元素del、pop()和remove()的區(qū)別,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Python具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-09-09