Python爬取整个网页的数据

淩亂°似流年 2022-04-17 05:19 550阅读 0赞

本案例是基于PyCharm开发的,也可以使用idea。
在项目内新建一个python文件Test.py
Test.py

  1. # 导入urllib下的request模块
  2. import urllib.request
  3. # 导入正则匹配包
  4. import re
  5. # -*- encoding:utf-8 -*-
  6. """ @作者:小思 @文件名:Test.py @时间:2018/11/13 14:42 @文档说明:测试爬虫(以爬取https://www.ittime.com.cn/news/zixun.shtml上的网页数据为例) """
  7. # 步骤
  8. # 1.确定要爬取数据的网址
  9. # 2.获取该网址的源码
  10. # 3.使用正则表达式去匹配网址的源码(匹配所需要的数据类型)
  11. # 4.将爬取的数据保存至本地或者数据库
  12. # 确定要爬取数据的网址
  13. url="https://www.ittime.com.cn/news/zixun.shtml"
  14. # 该网址的源码(以该网页的原编码方式进行编码,特殊字符编译不能编码就设置ignore)
  15. webSourceCode=urllib.request.urlopen(url).read().decode("utf-8","ignore")
  16. # 匹配数据的正则表达式
  17. # 所有的图片
  18. imgRe=re.compile(r'src="(.*?\.jpg)"')
  19. # 所有的标题
  20. titleRe=re.compile(r'<h2><a href=".*?" target="_blank">(.*?)</a></h2>')
  21. # 所有的简介
  22. contentRe=re.compile(r'<p>(.*?)</p>')
  23. # 所有的作者
  24. authorRe=re.compile(r'<span class="pull-left from_ori">(.*?)<span class="year">(.*?)</span></span>')
  25. # 匹配网页对应的标题数据
  26. titles=titleRe.findall(webSourceCode)
  27. images=imgRe.findall(webSourceCode)
  28. content=contentRe.findall(webSourceCode)
  29. authors=authorRe.findall(webSourceCode)
  30. print("标题==============================================================")
  31. for title in titles:
  32. print(title)
  33. print("图片==============================================================")
  34. for image in images:
  35. print("https://www.ittime.com.cn"+image)
  36. print("内容简介==============================================================")
  37. for c in content:
  38. print(c)
  39. print("作者==============================================================")
  40. for author in authors:
  41. print(author[0])
  42. print("时间==============================================================")
  43. for time in authors:
  44. print(author[1])

运行Test.py,控制台输出信息。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
如果分页的信息也全部需要,则写一个集合来保存这些需要读取数据的网址,将Test.py封装成方法。
在循环里依次调用
Test.py

  1. # 导入urllib下的request模块
  2. import urllib.request
  3. # 导入正则匹配包
  4. import re
  5. # -*- encoding:utf-8 -*-
  6. """ @作者:小思 @文件名:Test.py @时间:2018/11/13 14:42 @文档说明:测试爬虫(以爬取https://www.ittime.com.cn/news/zixun.shtml上的图片为例) """
  7. # 步骤
  8. # 1.确定要爬取数据的网址
  9. # 2.获取该网址的源码
  10. # 3.使用正则表达式去匹配网址的源码(匹配所需要的数据类型)
  11. # 4.将爬取的数据保存至本地或者数据库
  12. def getResouces(url):
  13. # 该网址的源码(以该网页的原编码方式进行编码,特殊字符编译不能编码就设置ignore)
  14. webSourceCode=urllib.request.urlopen(url).read().decode("utf-8","ignore")
  15. # 匹配数据的正则表达式
  16. # 所有的图片
  17. imgRe=re.compile(r'src="(.*?\.jpg)"')
  18. # 所有的标题
  19. titleRe=re.compile(r'<h2><a href=".*?" target="_blank">(.*?)</a></h2>')
  20. # 所有的简介
  21. contentRe=re.compile(r'<p>(.*?)</p>')
  22. # 所有的作者
  23. authorRe=re.compile(r'<span class="pull-left from_ori">(.*?)<span class="year">(.*?)</span></span>')
  24. # 匹配网页对应的标题数据
  25. titles=titleRe.findall(webSourceCode)
  26. images=imgRe.findall(webSourceCode)
  27. content=contentRe.findall(webSourceCode)
  28. authors=authorRe.findall(webSourceCode)
  29. print("标题==============================================================")
  30. for title in titles:
  31. print(title)
  32. print("图片==============================================================")
  33. for image in images:
  34. print("https://www.ittime.com.cn"+image)
  35. print("内容简介==============================================================")
  36. for c in content:
  37. print(c)
  38. print("作者==============================================================")
  39. for author in authors:
  40. print(author[0])
  41. print("时间==============================================================")
  42. for time in authors:
  43. print(author[1])
  44. # 读取前十页的数据
  45. for i in range(2,10):
  46. getResouces(f"https://www.ittime.com.cn/news/zixun_{i}.shtml")

注意!!!
①无论是java后台还是python后台需要大量的数据,都可以使用这种方式,它读取速度非常快,可以保存到本地,或者数据库。读取的时候要保持有网络哦~

②使用python爬取网页的数据并不困难,重要的是对你所需的数据的源代码的分析,要善于寻找规律,并且写出正确的正则表达式

说在最后的话:编写实属不易,若喜欢或者对你有帮助记得点赞+关注或者收藏哦~

发表评论

表情:
评论列表 (有 0 条评论,550人围观)

还没有评论,来说两句吧...

相关阅读