Python网络爬虫实习报告总结归纳Word下载.docx
《Python网络爬虫实习报告总结归纳Word下载.docx》由会员分享,可在线阅读,更多相关《Python网络爬虫实习报告总结归纳Word下载.docx(6页珍藏版)》请在冰豆网上搜索。
4>
元描述;
5>
元标签
五、数据爬取实战(豆瓣网爬取电影数据)
1分析网页
#获取html源代码
def__getHtml():
data=[]
pageNum=1
pageSize=0
try:
while(pageSize<
=125):
#'
Referer'
:
None#注意如果依然不能抓取的话,这里可以设置抓取网站的host
#}
#=[headers]
pageNum)
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,"
"
)
forliin("
ol"
attrs={'
class'
'
grid_view'
}).find_all("
li"
):
("
span"
class_="
title"
).text)
#("
div"
class_='
star'
).find("
rating_num'
pic'
em"
#spans=("
).find_all("
#forxinrange(len(spans)):
#ifx<
=2:
#pass
#else:
#(spans[x].string[-len(spans[x].string):
-3])
str=("
bd'
p"
'
).()
index=("
主"
if(index==-1):
..."
print("
if("
).text==210):
index=60
#print("
aaa"
#print(str[4:
index])
(str[4:
data['
title'
]=title
#data['
]=rating_num
range_num'
]=range_num
rating_people_num'
]=rating_people_num
movie_author'
]=movie_author
3数据整理、转换
def__getMovies(data):
html>
head>
metacharset='
UTF-8'
>
title>
Inserttitlehere<
/title>
/head>
body>
h1>
爬取豆瓣电影<
/h1>
h4>
作者:
刘文斌<
/h4>
时间:
+nowtime+"
hr>
tablewidth='
800px'
border='
1'
align=center>
thead>
tr>
th>
fontsize='
5'
color=green>
电影<
/font>
/th>
thwidth='
50px'
评分<
排名<
100px'
评价人数<
导演<
/tr>
/thead>
("
tbody>
fordataindatas:
foriinrange(0,25):
tdstyle='
color:
orange;
text-align:
center'
%s<
/td>
%data['
][i])
#("
blue;
red;
black;
/tbody>
/table>
/body>
/html>
()
if__name__=='
__main__'
datas=[]
htmls=__getHtml()
foriinrange(len(htmls)):
data=__getData(htmls[i])
(data)
__getMovies(datas)
4数据保存、展示
结果如后图所示:
5技术难点关键点
数据爬取实战(搜房网爬取房屋数据)
frombs4importBeautifulSoup
importrequests
rep=()
="
gb2312"
#设置编码方式
html=
soup=BeautifulSoup(html,'
f=open(,'
w'
encoding='
utf-8'
center>
新房成交TOP3<
/center>
tableborder='
1px'
width='
1000px'
height='
h2>
房址<
/h2>
成交量<
均价<
forliin("
ul"
class_="
ul02"
name=("
pbtext"
).text
chengjiaoliang=("
red-f3"
junjia=("
ohter"
gray-9"
)#.('
?
O'
'
平方米'
tdalign=center>
5px'
color=red>
%name)
color=blue>
%chengjiaoliang)
%junjia)
print(name)
六、总结
教师评语:
成绩:
指导教师: