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

首頁 > 編程 > Python > 正文

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

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

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

http://m.survivalescaperooms.com/article/83440.htmlhttp://m.survivalescaperooms.com/article/83437.htmlhttp://m.survivalescaperooms.com/article/83430.htmlhttp://m.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'

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 枞阳县| 松滋市| 延吉市| 洛隆县| 威远县| 衡阳市| 扶沟县| 五莲县| 疏勒县| 洛浦县| 西宁市| 嘉黎县| 扎鲁特旗| 连城县| 射阳县| 区。| 南江县| 井冈山市| 石城县| 德钦县| 台安县| 广河县| 濉溪县| 陆川县| 志丹县| 阿拉善盟| 泌阳县| 嵊泗县| 阜新市| 子洲县| 莲花县| 扬中市| 黑龙江省| 乡城县| 讷河市| 西盟| 玛沁县| 巨野县| 揭西县| 土默特右旗| 吴川市|