三个python爬虫项目实例代码
这篇文章主要介绍了三个python爬虫项目实例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
爬取内涵段子:
#encoding=utf-8 importurllib2 importre classneihanba(): defspider(self): ''' 爬虫的主调度器 ''' isflow=True#判断是否进行下一页 page=1 whileisflow: url="http://www.neihanpa.com/article/list_5_"+str(page)+".html" html=self.load(url) self.deal(html,page) panduan=raw_input("是否继续(y/n)!") ifpanduan=="y": isflow=True page+=1 else: isflow=False defload(self,url): ''' 针对url地址进行全部爬去 :paramurl:url地址 :return:返回爬去的内容 ''' header={ "User-Agent":"Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/67.0.3396.79Safari/537.36" } request=urllib2.Request(url,headers=header) response=urllib2.urlopen(request) html=response.read() returnhtml defdeal(self,html,page): ''' 对之前爬去的内容进行正则匹配,匹配出标题和正文内容 :paramhtml:之前爬去的内容 :parampage:正在爬去的页码 ''' parrten=re.compile('(https://www.haoziyuan.cc.*?)',re.S) titleList=parrten.findall(html) fortitleintitleList: parrten1=re.compile(' (.*)') ti1=parrten1.findall(title) parrten2=re.compile(' (https://www.haoziyuan.cc.*?)
","").replace("
","").replace("","").replace("
","").replace("&ldquo","\"").replace("&rdquo","\"") self.writeData(tr,page) defwriteData(self,context,page): ''' 将最终爬去的内容写入文件中 :paramcontext:匹配好的内容 :parampage:当前爬去的页码数 ''' fileName="di"+str(page)+"yehtml.txt" withopen(fileName,"a")asfile: file.writelines(context+"\n") if__name__=='__main__': n=neihanba() n.spider()
爬取智联:
#encoding=utf-8 importurllib importurllib2 importre classzhiLian(): defspider(self,position,workPlace): ''' 爬虫的主调度器 :paramposition:职位 :paramworkPlace:工作地点 ''' url="http://sou.zhaopin.com/jobs/searchresult.ashx?" url+=urllib.urlencode({"jl":workPlace}) url+="&" url+=urllib.urlencode({"kw":position}) isflow=True#是否进行下一页的爬去 page=1 whileisflow: url+="&"+str(page) html=self.load(url) self.deal1(html,page) panduan=raw_input("是否继续爬虫下一页(y/n)!") ifpanduan=="y": isflow=True page+=1 else: isflow=False defload(self,url): ''' 针对url地址进行全部爬去 :paramurl:url地址 :return:返回爬去的内容 ''' header={ "User-Agent":"Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/67.0.3396.79Safari/537.36" } request=urllib2.Request(url,headers=header) response=urllib2.urlopen(request) html=response.read() returnhtml defdeal1(self,html,page): ''' 对之前爬去的内容进行正则匹配,匹配职位所对应的链接 :paramhtml:之前爬去的内容 :parampage:正在爬去的页码 ''' parrten=re.compile('https://www.haoziyuan.cc.*?',re.S) til=parrten.findall(html)#爬去链接 fortintil: self.deal2(t,page) defdeal2(self,t,page): ''' 进行二次爬虫,然后在新的页面中对公司、薪资、工作经验进行匹配 :paramt:url地址 :parampage:当前匹配的页数 ''' html=self.load(t)#返回二次爬虫的内容 parrten1=re.compile('(https://www.haoziyuan.cc.*?)\s+https://www.haoziyuan.cc.*?',re.S) parrten2=re.compile('
爬取贴吧:
#encoding=utf-8 importurllib importurllib2 importre classteiba(): defspider(self,name,startPage,endPage): url="http://tieba.baidu.com/f?ie=utf-8&" url+=urllib.urlencode({"kw":name}) forpageinrange(startPage,endPage+1): pn=50*(page-1) urlFull=url+"&"+urllib.urlencode({"pn":pn}) html=self.loadPage(url) self.dealPage(html,page) defloadPage(self,url): header={ "User-Agent":"Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/67.0.3396.79Safari/537.36" } request=urllib2.Request(url,headers=header) response=urllib2.urlopen(request) html=response.read() returnhtml defdealPage(self,html,page): partten=re.compile(r'(https://www.haoziyuan.cc.*?)',re.S) titleList=partten.findall(html) rstr=r'#(https://www.haoziyuan.cc.*?)#' fortitleintitleList: title=re.sub(rstr,"",title) self.writePage(title,page) defwritePage(self,context,page): fileName="di"+str(page)+"yehtml.txt" withopen(fileName,"a")asfile: file.writelines(context+"\n") if__name__=='__main__': name=raw_input("请输入贴吧名:") startPage=raw_input("请输入起始页:") endPage=raw_input("请输入终止页:") t=teiba() t.spider(name,int(startPage),int(endPage))
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。