python爬虫程序,由根目录一直爬各个网址
来源:互联网 发布:手机淘宝怎么看优惠券 编辑:程序博客网 时间:2024/04/30 13:57
#coding=utf-8import urllib2import urllibimport reimport osfrom threading import Threadimport timeclass TimeoutException(Exception): passThreadStop = Thread._Thread__stop#获取私有函数def timelimited(timeout): def decorator(function): def decorator2(*args,**kwargs): class TimeLimited(Thread): def __init__(self,_error= None,): Thread.__init__(self) self._error = _error def run(self): try: self.result = function(*args,**kwargs) except Exception,e: self._error =e def _stop(self): if self.isAlive(): ThreadStop(self) t = TimeLimited() t.start() t.join(timeout) if isinstance(t._error,TimeoutException): t._stop() raise TimeoutException('timeout for %s' % (repr(function))) if t.isAlive(): t._stop() raise TimeoutException('timeout for %s' % (repr(function))) if t._error is None: return t.result return decorator2 return decorator@timelimited(5)def fn_1(url): data=urllib.urlopen(url).read() return dataurlbase='https://baidu.com'patt='a href=\"(http.+?)\"'p=re.compile(patt)def downHtml(url): count=1 backage='g:/downHtml/'+str(count) #os.mkdir(backage); path=backage+'/'+str(count)+'.html' request = urllib2.Request(url) #request = urllib2.Request('http://jandan.net/ooxx/page-1507#comments') request.add_header('User-Agent','Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6') opener = urllib2.build_opener() f= opener.open(request) doc=f.read() m=re.findall(p,doc) index=1 for i in m: print index,i index+=1 if len(m)>10000: break try: data=fn_1(i) except: continue if data is None: continue n=re.findall(p,data) setm=set(m) setn=set(n) n=setn-setm m.extend(n) for i in m: print index,i index+=1 '''for i in m: print index,urlbase+i index+=1 data=urllib.urlopen(url+i).read() n=re.findall('href="(\?.+?)"',data) f=open(path,'wb') f.write(data) f.close() count=count+1 path=backage+'/'+str(count)+'.html'''''downHtml(urlbase)print 'down'
0 0
- python爬虫程序,由根目录一直爬各个网址
- Python爬虫程序学习资料网址
- python爬虫学习网址
- Python的爬虫程序
- python爬虫程序
- python爬虫小程序
- python简单爬虫程序
- Python爬虫程序
- python爬虫程序-登录
- python爬虫helloworld程序
- 一个Python 爬虫程序
- [Python]多线程网址爬虫:控制线程数,爬虫深度
- Python 爬虫笔记(由站内到站外爬虫)
- python 调用外部程序防止一直阻塞
- python编写网络爬虫程序
- python简单网络爬虫程序
- 一个python爬虫小程序
- 简单的Python爬虫程序
- xcode编程中使用使用了DOUAudioStreamer这个第三方遇到的问题及其解决方法
- Java中泛型的理解(二)
- Codeforces Round #324 (Div. 2) B. Kolya and Tanya(组合数学)
- Handler自我理解
- acmPOJ--3615
- python爬虫程序,由根目录一直爬各个网址
- 女程序猿到此一游
- 连接数据库
- java之-命令模式
- 微信网页开发问题汇总
- 分布式RAM
- 第三十篇:控制器的生成与多控制器的管理
- 父类子类之间的一个经典题目(有关成员变量何时赋值和在哪里赋值的问题)
- 数塔DP