Python网络爬虫实习报告.docx
《Python网络爬虫实习报告.docx》由会员分享,可在线阅读,更多相关《Python网络爬虫实习报告.docx(8页珍藏版)》请在冰豆网上搜索。
Python网络爬虫实习报告
Python网络爬虫实习报告
Python网络爬虫实习(报告)
一、选题背景...................................................................................-2-二、爬虫原理...................................................................................-2-三、爬虫历史和分类........................................................................-2-四、常用爬虫框架比较....................................................................-2-五、数据爬取实战(豆瓣网爬取电影数据)...................................-3-1分析网页.....................................................................................-3-2爬取数据.....................................................................................-3-3数据整理、转换..........................................................................-4-4数据保存、展示..........................................................................-9-5技术难点关键点.........................................................................-10-
六、总结.........................................................................................-13-
-1-
Python网络爬虫实习(报告)
一、选题背景
二、爬虫原理
三、爬虫历史和分类
四、常用爬虫框架比较
Scrapy框架:
Scrapy框架是一套比较成熟的Python爬虫框架,是使用Python开发的快速、高层次的信息爬取框架,可以高效的爬取web页面并提取出结构化数据。
Scrapy应用范围很广,爬虫开发、数据挖掘、数据监测、自动化测试等。
Crawley框架:
Crawley也是Python开发出的爬虫框架,该框架致力于改变人们从互联网中提取数据的方式。
Portia框架:
Portia框架是一款允许没有任何编程基础的用户可视化地爬取网页的爬虫框架。
newspaper框架:
newspaper框架是一个用来提取新闻、文章以及内容分析的Python爬虫框架。
Python-goose框架:
Python-goose框架可提取的信息包括:
<1>文章主体内容;<2>文章主要图片;<3>文章中嵌入的任heYoutube/Vimeo视频;<4>元描述;<5>元标签
-2-
Python网络爬虫实习(报告)
五、数据爬取实战(豆瓣网爬取电影数据)
1分析网页
#获取html源代码
def__getHtml():
data=[]
pageNum=1
pageSize=0
try:
while(pageSize<=125):
#headers={'User-Agent':
'Mozilla/5.0(WindowsNT6.1)AppleWebKit/537.11(KHTML,likeGecko)
Chrome/23.0.1271.64Safari/537.11',
#'Referer':
None#注意如果依然不能抓取的话,这里
可以设置抓取网站的host
#}
#opener=urllib.request.build_opener()
#opener.addheaders=[headers]
url=";+
str(pageSize)+"&filter="+str(pageNum)
#data['html%s'%
i]=urllib.request.urlopen(url).read().decode("utf-8")
-3-
Python网络爬虫实习(报告)
data.append(urllib.request.urlopen(url).read().decode("utf-
8"))
pageSize+=25
pageNum+=1
print(pageSize,pageNum)
exceptExceptionase:
raisee
returndata
2爬取数据
def__getData(html):
title=[]#电影标题
#rating_num=[]#评分
range_num=[]#排名
#rating_people_num=[]#评价人数
movie_author=[]#导演
data={}
#bs4解析html
soup=BeautifulSoup(html,"html.parser")
forliinsoup.find("ol",attrs={'class':
'grid_view'}).find_all("li"):
-4-
Python网络爬虫实习(报告)
title.append(li.find("span",class_="title").text)
#rating_num.append(li.find("div",
class_='star').find("span",class_='rating_num').text)
range_num.append(li.find("div",
class_='pic').find("em").text)
#spans=li.find("div",
class_='star').find_all("span")
#forxinrange(len(spans)):
#ifx<=2:
#pass
#else:
#
rating_people_num.append(spans[x].string[-len(spans[x].stri
ng):
-3])
str=li.find("div",class_='bd').find("p",
class_='').text.lstrip()
index=str.find("主")
if(index==-1):
index=str.find("...")
print(li.find("div",
class_='pic').find("em").text)
if(li.find("div",class_='pic').find("em").text
-5-
Python网络爬虫实习(报告)
==210):
index=60
#print("aaa")
#print(str[4:
index])
movie_author.append(str[4:
index])
data['title']=title
#data['rating_num']=rating_num
data['range_num']=range_num
#data['rating_people_num']=rating_people_num
data['movie_author']=movie_author
returndata
3数据整理、转换
def__getMovies(data):
f=open('F:
//douban_movie.html','w',encoding='utf-8')
f.write("")
f.write("
Inserttitlehere
")f.write("
")f.write("
爬取豆瓣电影
")f.write("
作者:
刘文斌")
-6-
Python网络爬虫实习(报告)
f.write("
时间:
"+nowtime+"")
f.write("
")f.write("")
f.write("")f.write("
")f.write("
电影 | ")#f.write("
评分")
f.write("排
名")
#f.write("
评价人数")
f.write("
导演 | ")f.write("
")f.write("
")
f.write("
")fordataindatas:
foriinrange(0,25):
f.write("
")f.write("
-7- Python网络爬虫实习(报告) style='color: orange;text-align: center'>%s | "%data['title'][i])#f.write("
style='color: blue;text-align: center'>%s | "%data['rating_num'][i])
f.write("
style='color: red;text-align: center'>%s | "%data['range_num'][i])#f.write("
style='color: blue;text-align: center'>%s | "%data['rating_people_num'][i])
f.write("
style='color: black;text-align: center'>%s | "%data['movie_author'][i])f.write("
")f.write("
")f.write("")
f.write("")
f.write("")
f.write("")
f.close()
-8-
Python网络爬虫实习(报告)
if__name__=='__main__':
datas=[]
htmls=__getHtml()
foriinrange(len(htmls)):
data=__getData(htmls[i])
datas.append(data)
__getMovies(datas)
4数据保存、展示
结果如后图所示:
-9-
Python网络爬虫实习(报告)
5技术难点关键点
数据爬取实战(搜房网爬取房屋数据)
frombs4importBeautifulSoupimportrequests
rep=requests.get('')
rep.encoding="gb2312"#设置编码方式html=rep.text
soup=BeautifulSoup(html,'html.parser')
-10-
Python网络爬虫实习(报告)
f=open('F:
//fang.html','w',encoding='utf-8')
f.write("")
f.write("
Inserttitlehere
")f.write("
")f.write("
新房成交TOP3
")f.write("align=center>
")f.write("
房址 | ")f.write("
成交量 | ")f.write("
均价 |
")
forliinsoup.find("ul",class_="ul02").find_all("li"):
name=li.find("div",class_="pbtext").find("p").text
chengjiaoliang=li.find("span",class_="red-f3").text
try:
junjia=li.find("div",class_="ohter").find("p",class_="gray-9")#.text.replace(',O','平方米')
exceptExceptionase:
junjia=li.find("div",class_="gray-9")#.text.replace(',O','
-11-
Python网络爬虫实习(报告)
平方米')
f.write("
color=red>%s"%name)
f.write("%s"%chengjiaoliang)
f.write("%s
"%junjia)print(name)
f.write("")
f.write("")
-12-
Python网络爬虫实习(报告)六、总结
教师评语:
成绩:
指导教师:
-13-