摘要:本文主要向大家实例分享Python语言使用urllib2模块抓取HTML页面资源,通过具体的代码向大家展示,希望对大家学习Python语言有所帮助。
本文主要向大家实例分享Python语言使用urllib2模块抓取HTML页面资源,通过具体的代码向大家展示,希望对大家学习Python语言有所帮助。
先把要抓取的网络地址列在单独的list文件中
//www.300168.com/article/83440.html //www.300168.com/article/83437.html //www.300168.com/article/83430.html //www.300168.com/article/83449.html
然后我们来看程序操作,代码如下:
#!/usr/bin/python <d>import</d> os <d>import</d> sys <d>import</d> urllib2 <d>import</d> 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频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号