Python网络爬虫实习报告python实习报告

发布时间:2020-08-26 来源: 实习报告 点击:

  Python 网络爬虫实习报告

 目录 一、选题背景 .................................................................................... - 1 -

 二、爬虫原理 .................................................................................... - 1 -

 三、爬虫历史与分类 ......................................................................... - 1 -

 四、常用爬虫框架比较 ..................................................................... - 1 -

 五、数据爬取实战( 豆瓣网爬取电影数据) ........................................ - 2 -

 1 分析网页 .......................................................................................... -

 2

 - 2 爬取数据 .......................................................................................... -

 2

 - 3 数据整理、转换............................................................................... -

 3

 - 4 数据保存、展示............................................................................... -

 7

 - 5 技术难点关键点............................................................................... -

 9

 -

  六、总结 ......................................................................................... - 11 -

 一、

 选题背景 二、

 爬虫原理 三、

 爬虫历史与分类 四、

 常用爬虫框架比较 y Scrapy 框架: :Scrapy 框架就是一套比较成熟的 Python 爬虫框架,就是使用 Python 开发的快速、高层次的信息爬取框架,可以高效的爬取 web页面并提取出结构化数据。Scrapy 应用范围很广,爬虫开发、数据挖掘、数据监测、自动化测试等。

 y Crawley 框架: :Crawley 也就是 Python 开发出的爬虫框架,该框架致力于改变人们从互联网中提取数据的方式。

 a Portia 框架: :Portia 框架就是一款允许没有任何编程基础的用户可视化地爬取网页的爬虫框架。

 r newspaper 框架: :newspaper 框架就是一个用来提取新闻、文章以及内容分析的 Python 爬虫框架。

 Python- -e goose 框架: :Python-goose 框架可提取的信息包括:<1>文章主体内容;<2>文章主要图片;<3>文章中嵌入的任 heYoutube/Vimeo 视频;<4>元描述;<5>元标签

 五、数据爬取实战( 豆瓣网爬取电影数据) 1 分析网页

 # 获取 html 源代码

 def __getHtml():

 data = []

 pageNum = 1

 pageSize = 0

 try:

 while (pageSize <= 125):

 # headers = {"User- - Agent":"Mozilla/5 、 0 (Wi ndows NT 6 6 、 1) AppleWebKit/537 、 11 (KHTML, like Gecko) Chrome/23 、0 0 、1271 、 64 Safari/537 、 11",

 # "Referer":None # 注意如果依然不能抓取的话, , 这里可以设置抓取网站的 host

 # }

 # opener = urllib 、 request 、 build_opener()

 # opener 、 addheaders = [head ers]

 url = "" + str(pageSize) + "&filter=" + str(pageNum)

 # data["html%s" % i ]=urllib、 、 request、 、 urlopen(url) 、read() 、 decode("utf- - 8")

 data、 、 append(urllib、 、 request、 、 urlopen(url)、 、 read() 、

 decode("utf- - 8"))

 pageSize += 25 5

 pageNum += 1

 print(pageSize, pageNum)

 except Exception as e:

 raise e

 return data

 2 爬取数据 def __getData(html):

 title = []

 # 电影标题

 #rating_num = []

 # 评分

 range_num = []

  # 排名

 #rating_people_num = []

 #

 评价人数

 movie_author = []

  # 导演

 data = {}

 # bs4 解析 html

 soup = BeautifulSoup(html, "html 、 parser")

 for li in soup 、 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("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")

 #for x in range(len(sp ans)):

 #

  if x <= 2:

 #

 pass

 # else:

 #

 rating_people_num 、 append(spans[x] 、string[- - len(spans[x] 、 string):- - 3])

 str = li 、 find("div", class_="bd") 、 find("p", class_="") 、 text 、 lstrip()

 index = str 、 fi nd(" 主 ")

 if (index == - - 1):

 index = str 、 find(" 、、、 ")

 print(li 、 find("div", class_="pic") 、 find("em") 、text)

 if (li、 、 find("div", class_="pic")、 、 find("em")、 、 text == 210):

 index = 60

 # print("a aa")

 # 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"] = mov ie_author

 return data

 3 数据整理、转换 def __getMovies(data):

 f = open("F://douban_movie 、 html", "w",encoding="utf- - 8")

 f f 、 write("<html>")

 f f 、 write("<head><meta charset="UTF- - 8"><title>Insert title here</title></head>")

 f f 、 write("<body>")

 f f 、 write ("<h1> 爬取豆瓣电影 </h1>")

 f f 、 write("<h4> 作者: : 刘文斌 </h4>")

 f f 、 write("<h4> 时间 :" + nowtime + "</h4>")

 f f 、 write("<hr>")

 f f 、 write("<table width="800px" border="1" align=center>")

 f f 、 write("<thead>")

 f f 、 write("<tr>")

 f f 、 write("<th><font size="5" colo r=green> 电影</font></th>")

 #f 、 write("<th

 width="50px"><font size="5" color=green>评分 </font></th>")

 f f 、 write("<th

 width="50px"><font size="5" color=green>排名 </font></th>")

 #f 、 write("<th

 width="100px"><font size="5" color=green>评价人数 </font></th>")

 f f 、 write("<th><font size="5" color=green> 导演</font></th>")

 f f 、 write("</tr>")

 f f 、 write("</thead>")

 f f 、 write("<tbody>")

 for data in datas:

 for i in range(0, 25):

 f f 、 write("<tr>")

 f f 、 write("<td style="color:orange;text- - align:center">%s</td>" % data["title"][i])

 #

 f 、 write("<td

 style="color:blue;text- - align:center">%s</td>" % data["rating_num"][i])

 f f 、 write("<td style="color:red;text- - align:center">%s</td>" % data["range_num"][i])

 #

  f 、 write(" <td style="color:blue;text- - align:center">%s</td>" % data["rating_people_num"][i])

 f f 、 write("<td style="color:black;text- - align:center">%s</td>" % data["movie_author"][i])

 f f 、 write("</tr>")

 f f 、 write("</tbody>")

 f f 、 write("</thead>")

 f f 、 write("</table>")

 f f 、 write("</body>")

 f f 、 write("</html>")

 f f 、 close()

 if __name__ == "__main__":

 datas = []

 htmls = __getHtml()

 for i in range(len(htmls)):

 data = __getData(htmls[i])

 datas 、 append(data)

 __getMovies(datas)

  4 4 数据保存、展示

 结果如后图所示:

  5 技术难点关键点 数据爬取实战( 搜房网爬取房屋数据)

 from bs4 import BeautifulSoup import requests rep = requests、get( "") rep、encoding = "gb2312"

 # 设置编码方式 html = rep、text soup = BeautifulSoup(html, "html 、 parser") f = open( "F://fang 、 htm l", "w",encoding= "utf- - 8") f、write( "<html>") f、write( "<head><meta charset="UTF- - 8"><title>Insert title here</title></head>") f、write( "<body>") f、write( "<center><h1> 新房成交 TOP3</h1></center>") f、write( "<table border="1px" width="1000px" height="800px" align=cent er><tr>") f、write( "<th><h2> 房址 </h2></th>") f、write( "<th><h2> 成交量 </h2></th>") f、write( "<th><h2> 均价 </h2></th></tr>") for li in soup、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", "平方米")

  except Exception as e:

  junjia=li、find( "div",class_= "gray- - 9") #、text、replace("?O", "平方米")

 f、write( "<tr><td ali gn=center><font size="5px"

 color=red>%s</font></td>" % name)

  f、write( "<td align=center><font size="5px"

 color=blue>%s</font></td>" % chengjiaoliang)

  f、write( "<td align=center><font size="5px"

 color=green>%s</font></td></tr>" % junjia)

  print(name)

 f、write( "</table>") f、write( "</body>")

 六、总结

  教师评语:

  成绩:

  指导教师:

相关热词搜索:实习报告 爬虫 网络

版权所有 蒲公英文摘 www.zhaoqt.net