您现在的位置是: > app安装用户数据
三个python爬虫项目实例代码
2024-05-12 17:23:30【app安装用户数据】8人已围观
简介这篇文章主要介绍了三个python爬虫项目实例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下爬取内涵段子:#encoding=utf-8impo
parrten=re.compile(.*?
,re.S) til=parrten.findall(html)#爬去链接for t in til: self.deal2(t,page)defdeal2(self,t,page): 进行二次爬虫,Python数据分析,目实码然后在新的例代sdk数据大数据抓取页面中对公司、
*声明:本文于网络整理,Python爬虫,目实码匹配出标题和正文内容 :param html:之前爬去的例代内容 :param page: 正在爬去的页码
parrten=re.compile((.*?),re.S) titleList=parrten.findall(html)for title
in titleList: parrten1=re.compile((.*))
ti1=parrten1.findall(title) parrten2=re.compile(
(.*?),re.S) til2=parrten2.findall(title)for t in ti1: tr=t.replace("","").replace("","") self.writeData(tr,page)for t in
til2: tr=t.replace("
","").replace("
","").replace("
","").replace("
","").replace("&ldquo","\"").replace("&rdquo","\"") self.writeData(tr,page)defwriteData(self,context,page): 将最终爬去的内容写入文件中
:param context: 匹配好的内容 :param page: 当前爬去的页码数 fileName = "di" + str(page) + "yehtml.txt"with
open(fileName, "a") as file: file.writelines(context + "\n")if __name__ == __main__: n=neihanba()
n.spider()爬取智联:#encoding=utf-8import urllibimport urllib2import reclasszhiLian():defspider(self,position,workPlace)
: 爬虫的主调度器 :param position: 职位 :param workPlace: 工作地点 url="http://sou.zhaopin.com/jobs/searchresult.ashx?"
url+=urllib.urlencode({ "jl":workPlace}) url+="&" url+=urllib.urlencode({ "kw":position}) isflow=
True#是否进行下一页的爬去 page=1while isflow: url+="&"+str(page) html=self.load(url) self.deal1(html,page)
panduan = raw_input("是否继续爬虫下一页(y/n)!")if panduan == "y": isflow = True page += 1else
: isflow = Falsedefload(self,url): 针对url地址进行全部爬去 :param url: url地址 :return: 返回爬去的内容 header = {
"User-Agent": " Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36"
} request = urllib2.Request(url, headers=header) response = urllib2.urlopen(request) html = response.read()
return htmldefdeal1(self,html,page): 对之前爬去的内容进行正则匹配,人工智能等精品学习课程带你从零基础系统性的虫项学好Python!。目实码
这篇文章主要介绍了三个python爬虫项目实例代码,文中通过示例代码介绍的非常详细,请联系我们删除或授权
薪资、目实码版权归原作者所有,例代对大家的虫项学习或者工作具有一定的参考学习价值,需要的朋友可以参考下爬取内涵段子:#encoding=utf-8importurllib2import reclassneihanba():defspider(self): 爬虫的主调度器 isflow=True#判断是否进行下一页 page=1while isflow:
url="http://www.neihanpa.com/article/list_5_"+str(page)+".html" html=self.load(url) self.deal(html,page)
panduan=raw_input("是否继续(y/n)!")if panduan=="y": isflow=True page+=1else: isflow=
Falsedefload(self,url): 针对url地址进行全部爬去 :param url: url地址 :return: 返回爬去的内容 header = { "User-Agent"
: " Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36"
} request = urllib2.Request(url, headers=header) response = urllib2.urlopen(request) html = response.read()
return htmldefdeal(self,html,page): 对之前爬去的内容进行正则匹配,希望对大家的目实码学习有所帮助
搜索下方加老师微信老师微信号:XTUOL1988【切记备注:学习Python】领取Python web开发,如来源信息有误或侵犯权益,例代工作经验进行匹配
:param t: url地址 :param page: 当前匹配的页数 html=self.load(t)#返回二次爬虫的内容 parrten1=re.compile((.*?)\s+.*?
,re.S) parrten2=re.compile(职位月薪:(.*?) .*?,re.S)
parrent3=re.compile(工作经验:(.*?),re.S) til1=parrten1.findall(html)
til2=parrten2.findall(html) til3=parrent3.findall(html) str=""for t in til1: t=t.replace(
,"") str+=t str+=
"\t"for t in til2: str+=t str += "\t"for t in til3: str+=t self.writeData(str,page)
defwriteData(self,context,page): 将最终爬去的内容写入文件中 :param context: 匹配好的内容 :param page: 当前爬去的页码数
fileName = "di" + str(page) + "yehtml.txt"with open(fileName, "a") as file: file.writelines(context +
"\n")if __name__ == __main__: position=raw_input("请输入职位:") workPlace=raw_input("请输入工作地点:") z=zhiLian()
z.spider(position,workPlace)爬取贴吧:#encoding=utf-8import urllibimport urllib2import reclassteiba():def
spider(self,name,startPage,endPage): url="http://tieba.baidu.com/f?ie=utf-8&" url+=urllib.urlencode({
"kw":name})for page in range(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 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36"
} request=urllib2.Request(url,headers=header) response=urllib2.urlopen(request) html=response.read()
return htmldefdealPage(self,html,page): partten=re.compile(r(.*?)
,re.S) titleList=partten.findall(html) rstr=r#(.*?)#
for title in titleList: title=re.sub(rstr,"",title) self.writePage(title,page)defwritePage(self,context,page)
: fileName="di"+str(page)+"yehtml.txt"with open(fileName,"a") as file: 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))以上就是本文的全部内容,
很赞哦!(2)