#!/usr/bin/env python

#coding=utf-8

importthreadingimporturllibimportreimporttime

g_mutex=threading.Condition()

g_pages=[] #从中解析所有url链接

g_queueURL=[] #等待爬取的url链接列表

g_existURL=[] #已经爬取过的url链接列表

g_failedURL=[] #下载失败的url链接列表

g_totalcount=0#下载过的页面数classCrawler:

def __init__(self,crawlername,url,threadnum):

self.crawlername=crawlername

self.url=url

self.threadnum=threadnum

self.threadpool=[]

self.logfile=file("log.txt",'w')

def craw(self):

global g_queueURL

g_queueURL.append(url)

depth=0print self.crawlername+" 启动..."

while(len(g_queueURL)!=0):

depth+=1print'Searching depth ',depth,'...\n\n'self.logfile.write("URL:"+g_queueURL[0]+"........")

self.downloadAll()

self.updateQueueURL()

content='\n>>>Depth '+str(depth)+':\n'self.logfile.write(content)

i=0

while i

content=str(g_totalcount+i)+'->'+g_queueURL[i]+'\n'self.logfile.write(content)

i+=1def downloadAll(self):

global g_queueURL

global g_totalcount

i=0

while i

j=0

while j

g_totalcount+=1threadresult=self.download(g_queueURL[i+j],str(g_totalcount)+'.html',j)if threadresult!=None:

print'Thread started:',i+j,'--File number =',g_totalcount

j+=1i+=jforthread in self.threadpool:

thread.join(30)

threadpool=[]

g_queueURL=[]

def download(self,url,filename,tid):

crawthread=CrawlerThread(url,filename,tid)

self.threadpool.append(crawthread)

crawthread.start()

def updateQueueURL(self):

global g_queueURL

global g_existURL

newUrlList=[]forcontent in g_pages:

newUrlList+=self.getUrl(content)

g_queueURL=list(set(newUrlList)-set(g_existURL))

def getUrl(self,content):

reg=r'"(http://.+?)"'regob=re.compile(reg,re.DOTALL)

urllist=regob.findall(content)returnurllistclassCrawlerThread(threading.Thread):

def __init__(self,url,filename,tid):

threading.Thread.__init__(self)

self.url=url

self.filename=filename

self.tid=tid

def run(self):

global g_mutex

global g_failedURL

global g_queueURLtry:

page=urllib.urlopen(self.url)

html=page.read()

fout=file(self.filename,'w')

fout.write(html)

fout.close()

except Exception,e:

g_mutex.acquire()

g_existURL.append(self.url)

g_failedURL.append(self.url)

g_mutex.release()

print'Failed downloading and saving',self.url

print ereturnNone

g_mutex.acquire()

g_pages.append(html)

g_existURL.append(self.url)

g_mutex.release()if __name__=="__main__":

url=raw_input("请输入url入口:\n")

threadnum=int(raw_input("设置线程数:"))

crawlername="小小爬虫"crawler=Crawler(crawlername,url,threadnum)

crawler.craw()

python多线程爬虫界面_多线程网页爬虫 python 实现相关推荐

  1. python html做界面_如何用EEL(python库)打开一个新的HTML页面?

    我刚刚开始学习所有的编程知识,偶然发现了这个Python库"EEL",在这个库中,您可以用HTML创建一个像样的GUI.然而,可供学习的教程或像样的示例项目并不多.这是我想要做的, ...

  2. python做动态相册_动感网页相册 python编写简单文件夹内图片浏览工具

    ''' template_body = '' # 如果指定乱序,就乱序列表中的数据 if shuffle == True: from random import shuffle shuffle(fil ...

  3. Python十分适合用来开发网页爬虫

    Python十分适合用来开发网页爬虫,理由如下: 1.抓取网页自身的接口 比较与其他静态编程语言,如java,c#,c++,python抓取网页文档的接口更简练:比较其他动态脚本语言,如perl,sh ...

  4. python正则表达式提取电话号码_利用正则表达式编写python 爬虫,抓取网页电话号码!...

    利用正则表达式编写python 爬虫,抓取网页联系我们电话号码!这里以九奥科技(www.jiuaoo.com)为例,抓取'联系我们'里面的电话号码,并输出. #!/usrweilie/bin/pyth ...

  5. 【Python 爬虫】简单的网页爬虫

    这边有一个用来测试的网站点击跳转 简单的网页爬虫 requests的使用 使用requests获取网页的源代码 requests与正则结合 多线爬虫 多进程库 开发多线程爬虫 爬虫算法的开发 深度优先 ...

  6. 常见python爬虫框架_常用高效的Python爬虫框架

    Python是现在非常流行的编程语言,而爬虫则是Python语言中最典型的应用,下面是ipidea全球http总结的高效Python爬虫框架,大家看看是否都用过呢. Scrapy是一个为了爬取网站数据 ...

  7. python 项目实战视频_腾讯视频 Python 爬虫项目实战

    做了一些小项目,用的技术和技巧会比较散比较杂,写一个小品文记录一下,帮助熟悉. 需求:经常在腾讯视频上看电影,在影片库里有一个"豆瓣好评"板块.我一般会在这个条目下面挑电影.但是电 ...

  8. python 制作简单网站_新手小白 做python爬虫 爬什么网站比较简单?

    本篇文章就新手小白来说,教大家怎么爬虫.现学现卖,看完再自己操作操作就会了~我就是这么学的,分享给想用python爬虫的小伙伴: 放个懒人目录:网络爬虫的行径 URL初步的概念 python与urll ...

  9. 潭州教育可以学python爬虫吗?_什么是爬虫?其运行原理是什么?只有Python才可以吗?...

    爬虫是什么? 本文所说的爬虫不是自然界中的爬行动物,而是运行在网络中的一个自动处理信息的程序. 爬虫是一个程序,该程序利用网络请求(HTTP/HTTPS)进行数据的筛选和录入.因为网络信息维度很广泛, ...

最新文章

  1. mysql安装教程8.0.21安装,mysql 8.0.21 安装配置方法图文教程
  2. 牛客网 -- 计算机历年考研复试上机题 -- 手机键盘
  3. 服务器被黑 追寻ip_我的服务器被打死,源IP暴露怎么办补救
  4. php类和对象-作用,php 类和对象
  5. 古希腊的五大数学巨匠
  6. 5G 重新定义生老病死!
  7. 在OL6.5_64上安装Oracle11gR2_64
  8. 困难之下见证良心公司!!
  9. Rem实现自适应初体验
  10. POI数据获取-Python
  11. 2019年终总结-坚持的力量
  12. win10右键卡顿原因_右键菜单反应慢?win10系统解决右键菜单卡顿方法
  13. postman导入postman_collection文件
  14. vue-cli项目代理proxyTable配置exclude的方法
  15. 用C++编写一个简易数学计算器
  16. C语言中关于位域的介绍
  17. Linux系统中rpm命令用法详解
  18. Autodesk Inventor Publisher 2013安装破解
  19. [书目20100320]首先,打破一切常规
  20. VUE+bpmn.js+iview 页面绘制流程图

热门文章

  1. java 操作 mongodb_Java操作MongoDB数据库方法详解
  2. vue切换class_Vue点击切换Class变化,实现Active当前样式操作
  3. 科技论文中的分析与综合-如何写好科技论文之我见(七)
  4. Codeforces 798D Mike and distribution (构造)
  5. Codeforces 919D Substring (拓扑图DP)
  6. c语言如何设置c99标准,新的C语言: C99标准介绍
  7. java spring框架 注解_详解Java的Spring框架中的注解的用法
  8. 无障碍开发(四)之ARIA aria-***状态值
  9. CF1070L Odd Federalization 高斯消元
  10. 2018/3/18 noip模拟赛 20分