Python网络爬虫实习报告Word文件下载.docx
《Python网络爬虫实习报告Word文件下载.docx》由会员分享,可在线阅读,更多相关《Python网络爬虫实习报告Word文件下载.docx(7页珍藏版)》请在冰点文库上搜索。
newspaper框架是一个用来提取新闻、文章以及内容分析的Python爬虫框架。
Python-goose框架:
Python-goose框架可提取的信息包括:
<
1>
文章主体内容;
2>
文章主要图片;
3>
文章中嵌入的任heYoutube/Vimeo视频;
4>
元描述;
5>
元标签
五、数据爬取实战(豆瓣网爬取电影数据)
1分析网页
#获取html源代码
def__getHtml():
data=[]
pageNum=1
pageSize=0
try:
while(pageSize<
=125):
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"
):
title.append(li.find("
span"
class_="
title"
).text)
#rating_num.append(li.find("
div"
class_='
star'
).find("
rating_num'
range_num.append(li.find("
pic'
em"
#spans=li.find("
).find_all("
#forxinrange(len(spans)):
#ifx<
=2:
#pass
#else:
#rating_people_num.append(spans[x].string[-len(spans[x].string):
-3])
str=li.find("
bd'
p"
'
).text.lstrip()
index=str.find("
主"
if(index==-1):
..."
print(li.find("
if(li.find("
).text==210):
index=60
#print("
aaa"
#print(str[4:
index])
movie_author.append(str[4:
data['
title'
]=title
#data['
]=rating_num
range_num'
]=range_num
rating_people_num'
]=rating_people_num
movie_author'
]=movie_author
returndata
3数据整理、转换
f.write("
thead>
"
tr>
th>
fontsize='
5'
color=green>
电影<
/font>
/th>
#f.write("
thwidth='
50px'
>
评分<
排名<
100px'
评价人数<
导演<
/tr>
/thead>
f.write("
tbody>
fordataindatas:
foriinrange(0,25):
tdstyle='
color:
orange;
text-align:
center'
%s<
/td>
%data['
][i])
#f.write("
blue;
red;
black;
/tbody>
/table>
/body>
/html>
f.close()
if__name__=='
__main__'
datas=[]
htmls=__getHtml()
foriinrange(len(htmls)):
data=__getData(htmls[i])
datas.append(data)
__getMovies(datas)
4数据保存、展示
结果如后图所示:
5技术难点关键点
数据爬取实战(搜房网爬取房屋数据)
frombs4importBeautifulSoup
importrequests
rep=requests.get()
rep.encoding="
gb2312"
#设置编码方式
html=rep.text
soup=BeautifulSoup(html,'
html.parser'
f=open(,'
w'
encoding='
utf-8'
html>
head>
metacharset='
UTF-8'
title>
Inserttitlehere<
/title>
/head>
body>
center>
h1>
新房成交TOP3<
/h1>
/center>
tableborder='
1px'
width='
1000px'
height='
800px'
align=center>
h2>
房址<
/h2>
成交量<
均价<
forliinsoup.find("
ul"
class_="
ul02"
name=li.find("
pbtext"
).text
chengjiaoliang=li.find("
red-f3"
junjia=li.find("
ohter"
gray-9"
)#.text.replace('
?
O'
'
平方米'
exceptExceptionase:
tdalign=center>
5px'
color=red>
%name)
color=blue>
%chengjiaoliang)
%junjia)
print(name)
六、总结
教师评语:
成绩:
指导教师: