- #FileName:toolbox_insight.py
- fromsgmllibimportSGMLParser
- importthreading
- importtime
- importurllib2
- importStringIO
- importgzip
- importstring
- importos
- #rewriteSGMLParserforstart_a
- classBasegeturls(SGMLParser):#这个Basegeturls类作用是分析下载的网页,把网页中的所有链接放在self.url中。
- defreset(self):
- self.url=[]
- SGMLParser.reset(self)
- defstart_a(self,attrs):
- href=[vfork,vinattrsifk=='href']
- ifhref:
- self.url.extend(href)
- #forquicklyfinding
- classNewlist(list):#这个类其实是一个添加了find方法的LIST。当num变量在LIST中,返回True,当不在LIST中,返回False并把num按二分法插入LIST中
- deffind(self,num):
- l=len(self)
- first=0
- end=l-1
- mid=0
- ifl==0:
- self.insert(0,num)
- returnFalse
- whilefirst<end:
- mid=(first+end)/2
- ifnum>self[mid]:
- first=mid+1
- elifnum<self[mid]:
- end=mid-1
- else:
- break
- iffirst==end:
- ifself[first]>num:
- self.insert(first,num)
- returnFalse
- elifself[first]<num:
- self.insert(first+1,num)
- returnFalse
- else:
- returnTrue
- eliffirst>end:
- self.insert(first,num)
- returnFalse
- else:
- returnTrue
- #下面的reptile顾名思义是一个爬虫
- classreptile(threading.Thread):
- #Name:是爬虫是名字,queue是任务队列,所有的爬虫共用同一个任务队列
- #从中取出一个任务项进行运行,每个任务项是一个要下载网页的URL
- #result:也是一个队列,将下载的网页中包含的URL放入该队列中
- #inittime:在本程序中没有用,只是一个为了以后扩展用的
- #downloadway:是下载的网页存放的路径
- #configfile:是配置文件,存放网页的URL和下载下后的路径
- #maxnum:每个爬虫有个最大下载量,当下载了这么多网页后,爬虫dead
- def__init__(self,Name,queue,result,Flcok,inittime=0.00001,downloadway='D:\\bbs\\',configfile='D:\\bbs\\conf.txt',maxnum=10000):
- threading.Thread.__init__(self,name=Name)
- self.queue=queue
- self.result=result
- self.Flcok=Flcok
- self.inittime=inittime
- self.mainway=downloadway
- self.configfile=configfile
- self.num=0#已下载的网页个数
- self.maxnum=maxnum
- os.makedirs(downloadway+self.getName())#系统调用:在存放网页的文件夹中创建一个以该爬虫name为名字的文件夹
- self.way=downloadway+self.getName()+'\\'
- defrun(self):
- opener=urllib2.build_opener()#创建一个开启器
- whileTrue:
- url=self.queue.get()#从队列中取一个URL
- ifurl==None:#当取得一个None后表示爬虫结束工作,用于外部方便控制爬虫的生命期
- break
- parser=Basegeturls()#创建一个网页分析器
- request=urllib2.Request(url)#网页请求
- request.add_header('Accept-encoding','gzip')#下载的方式是gzip压缩后的网页,gzip是大多数服务器支持的一种格式
- try:#这样可以减轻网络压力
- page=opener.open(request)#发送请求报文
- ifpage.code==200:#当请求成功
- predata=page.read()#下载gzip格式的网页
- pdata=StringIO.StringIO(predata)#下面6行是实现解压缩
- gzipper=gzip.GzipFile(fileobj=pdata)
- try:
- data=gzipper.read()
- except(IOError):
- print'unusedgzip'
- data=predata#当有的服务器不支持gzip格式,那么下载的就是网页本身
- try:
- parser.feed(data)#分析网页
- except:
- print'Iamhere'#有的网页分析不了,如整个网页就是一个图片
- foriteminparser.url:
- self.result.put(item)#分析后的URL放入队列中
- way=self.way+str(self.num)+'.html'#下面的是网页的保存,不多说了
- self.num+=1
- file=open(way,'w')
- file.write(data)
- file.close()
- self.Flcok.acquire()
- confile=open(self.configfile,'a')
- confile.write(way+''+url+'\n')
- confile.close()
- self.Flcok.release()
- page.close()
- ifself.num>=self.maxnum:#达到最大量后退出
- break
- except:
- print'enderror'
- #和爬虫一样是个线程类,作用是将爬虫中的result中存入的URL加以处理。只要同一个服务器的网页
- classproinsight(threading.Thread):
- def__init__(self,queue,list,homepage,inqueue):
- threading.Thread.__init__(self)
- self.queue=queue#和爬虫中的result队列是同一个
- self.list=list#是上面Newlist的对象
- self.homepage=homepage#主页
- self.inqueue=inqueue#处理完后的URL的去处
- defrun(self):
- length=len(self.homepage)
- whileTrue:
- item=self.queue.get()
- ifitem==None:
- break
- ifitem[0:4]=='\r\n':
- item=item[4:]
- ifitem[-1]=='/':
- item=item[:-1]
- iflen(item)>=len('http://')anditem[0:7]=='http://':
- iflen(item)>=lengthanditem[0:length]==self.homepage:
- ifself.list.find(item)==False:
- self.inqueue.put(item)
- elifitem[0:5]=='/java'oritem[0:4]=='java':
- pass
- else:
- ifitem[0]!='/':
- item='/'+item
- item=self.homepage+item
- ifself.list.find(item)==False:
- self.inqueue.put(item)
- 下面的是一个主函数过程
- 我下载的网站是http://bbs.hit.edu.cn
- 开始网页是http://bbs.hit.edu.cn/mainpage.php
- #FileName:test
- fromtoolbox_insightimport*
- fromQueueimportQueue
- importthreading
- importsys
- num=int(raw_input('Enterthenumberofthread:'))
- pnum=int(raw_input('Enterthenumberofdownloadpages:'))
- mainpage=str(raw_input('Themainpage:'))
- startpage=str(raw_input('Startpage:'))
- queue=Queue()
- key=Queue()
- inqueue=Queue()
- list=Newlist()
- thlist=[]
- Flock=threading.RLock()
- foriinrange(num):
- th=reptile('th'+str(i),queue,key,Flock)
- thlist.append(th)
- pro=proinsight(key,list,mainpage,inqueue)
- pro.start()
- foriinthlist:
- i.start()
- queue.put(startpage)
- foriinrange(pnum):
- queue.put(inqueue.get())
- foriinrange(num):
- queue.put(None)
相关推荐
这通常通过HTTP请求库实现,如Python中的Requests库。 解析内容: 爬虫对获取的HTML进行解析,提取有用的信息。常用的解析工具有正则表达式、XPath、Beautiful Soup等。这些工具帮助爬虫定位和提取目标数据,如文本...
Web penetration testing is the use of tools and code to attack a website or web app in order to assess its vulnerability to external threats. While there are an increasing number of sophisticated, ...
Spider in the forest 156 Moving band of images 160 Continuous band of images 162 Endless background 164 Chapter 8: Data In and Data Out 167 Introduction 167 Creation of a new file on a hard ...
File "C:\Users\xieqianyun\AppData\Local\Programs\Python\Python36\lib\site-packages\IPython\core\interactiveshell.py", line 3319, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File...
作品主要结构(代码在 SourceCode 目录内): 数据采集:spider 后端:orfd 前端:website 核心环境配置 进入 ExecutableFiles 目录 安装 Python3: 选择 Python 3.6.8 安装 详细介绍参考:...
#https://www.runoob.com/w3cnote/python-spider-intro.html #Python 爬虫介绍 import urllib.parse import urllib.request from http import cookiejar url = http://www.baidu.com response1 = urllib.request....
code_technique 代码技巧 doc_operate 常规文件操作 pdf_operate pdf操作 word_operate word操作 spider 爬虫 gzh 公众号 wechat_data 微信数据 config 爬虫一般配置 down_video 下载视频 get_news 下载新闻 movie_...
使用python的scrapy爬取文本保存为txt文件 编码工具 Visual Studio Code 实现步骤 1.创建scrapyTest项目 在vscode中新建终端并依次输入下列代码: scrapy startproject scrapyTest cd scrapyTest code 打开项目...
python spider crawl demo 4年前
RequirementsPython3MysqlrequestslxmlpymysqltimeosQuick Start执行建表语句修改database.py中的数据库连接配置命令行执行python3 spider.pyFeature数据来源于国家统计局,网址: ,总共846462条数据,记录了全中国...
前言 在Python中,装饰器是一种十分强大并且好用的语法,一些重复的代码使用装饰器语法的话能够使代码更容易理解及阅读。 因此在这里简单总结了一下Python中装饰器的几种用法以及需要注意的事情。... print(code: 400)
lagou_position_spider.py: 任意输入关键字,一键抓取与关键字相关的职位招聘信息,并保存到本地文件。 student_img.py: 自动化获取自己学籍证件照。 JD_spider.py: 大批量抓取京东商品id和标签。 ECUT_pos_...
Etherscan_spider 爬取所有的智能合约代币的二进制文件并保存在tokens_code目录下 命名规则:token_name.sol 部分代码copy自 用法: python3 Etherscan_spider.py
leetcode中国Python 中的 Leet 代码 新手程序员从哪里开始 Codeacademy在线教程 ...Code。 Python 蜘蛛 GitHub 视觉工作室代码 实践 力码当满足计算约束时成为技巧 顶尖大学公开课 麻省理工 - 实验 1-8
在之前文章给大家分享后不久,就有位小伙伴跟小编说在用scrapy搭建python爬虫中出现错误了。一开始的时候小编也没有看出哪里有问题,好在经过不断地讨论与测试,最终解决了出错点的问题。有同样出错的小伙伴可要好好...
Python 3.6.0以上 适用于Linux,Windows,macOS 安装 从PyPi: pip3 install feapder 从Git: pip3 install git+https://github.com/Boris-code/feapder.git 若安装出错,请参考 小试一下 创建爬虫 feapder ...
code_space_spider_demo ├─html_downloader.py 网页下载器 ├─html_outputer.py 数据处理器 ├─html_parser.py 网页解析器 ├─output.html 数据展示html ├─spider_main.py 爬虫启动器 ├─urls_...
- python 3.0+ - pip install -r requirements.txt 2.将最第二层stock_spider文件夹设置为根目录 3.将firefox驱动程序包放到python环境的Scripts文件夹里 4.必须要安装firefox浏览器才会调用到浏览器 5.执行...
- python 3.0+ - pip install -r requirements.txt 2.将最第二层stock_spider文件夹设置为根目录,pycharm的mark dir as sources root 3.将firefox驱动程序包放到python环境的Scripts文件夹里 4.必须要安装fire...