国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python使用urllib2模塊抓取HTML頁面資源的實例分享

2020-01-04 17:30:02
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了Python使用urllib2模塊抓取HTML頁面資源的實例分享,將要抓取的頁面地址寫在單獨的規則列表中方便組織和重復使用,需要的朋友可以參考下
 

先把要抓取的網絡地址列在單獨的list文件中

http://www.survivalescaperooms.com/article/83440.htmlhttp://www.survivalescaperooms.com/article/83437.htmlhttp://www.survivalescaperooms.com/article/83430.htmlhttp://www.survivalescaperooms.com/article/83449.html

然后我們來看程序操作,代碼如下:

#!/usr/bin/pythonimport osimport sysimport urllib2import redef 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'

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 成安县| 曲靖市| 营口市| 九寨沟县| 黔西| 肇庆市| 将乐县| 庄浪县| 富裕县| 阆中市| 汽车| 商水县| 潼关县| 通道| 镇坪县| 乐亭县| 西贡区| 桦甸市| 奎屯市| 灵川县| 南京市| 湘乡市| 辽宁省| 左贡县| 镇赉县| 大庆市| 武宁县| 桐乡市| 渝北区| 司法| 基隆市| 伽师县| 镇康县| 璧山县| 德保县| 沁水县| 岳池县| 丁青县| 天峨县| 梁平县| 金溪县|