网页爬虫抓取百度图片(《凡人修仙传吧_*敏*感*词*》之中制作原理基本相同)

优采云 发布时间: 2021-11-22 12:25

  网页爬虫抓取百度图片(《凡人修仙传吧_*敏*感*词*》之中制作原理基本相同)

  *敏*感*词*的爬虫制作原理与Embarrassed爬虫制作原理基本一致。他们都通过查看源代码提取关键数据,然后将其存储在本地txt文件中。

  源码下载:

  项目内容:

  *敏*感*词*用Python编写的网络爬虫。

  使用方法:

  新建一个BugBaidu.py文件,将代码复制进去,双击运行。

  程序功能:

  将贴吧宿主发布的内容打包成txt打包保存到本地

  原理说明:

  首先先浏览某篇文章贴吧,点击只看到海报,点击第二页,url有点变了,变成了:

  可以看出see_lz=1是只看主机,pn=1是对应的页码。记住这一点,为以后的写作做好准备。

  这是我们需要使用的网址。

  下一步是查看页面的源代码。

  在第一次选择标题和存储文件时会用到它。

  百度标题贴吧:《修仙传说》中的一些“路人”故事_修仙故事_*敏*感*词*

  源代码:

   1 # -*- coding: utf-8 -*-

2 #---------------------------------------

3 # 程序:*敏*感*词*爬虫

4 # 版本:0.5

5 # 作者:fjl

6 # 日期:2016-2-11

7 # 语言:Python 2.7

8 # 操作:输入网址后自动只看楼主并保存到本地文件

9 # 功能:将楼主发布的内容打包txt存储到本地。

10 #---------------------------------------

11

12 import string

13 import urllib2

14 import re

15

16 #----------- 处理页面上的各种标签 -----------

17 class HTML_Tool:

18 # 用非 贪婪模式 匹配 \t 或者 \n 或者 空格 或者 超链接 或者 图片

19 BgnCharToNoneRex = re.compile("(\t|\n| ||)")

20

21 # 用非 贪婪模式 匹配 任意标签

22 EndCharToNoneRex = re.compile("")

23

24 # 用非 贪婪模式 匹配 任意<p>标签

25 BgnPartRex = re.compile("")

26 CharToNewLineRex = re.compile("(

||||)")

27 CharToNextTabRex = re.compile("")

28

29 # 将一些html的符号实体转变为原始符号

30 replaceTab = [("",">"),("&","&"),("&","\""),(" "," ")]

31

32 def Replace_Char(self,x):

33 x = self.BgnCharToNoneRex.sub("",x)

34 x = self.BgnPartRex.sub("\n ",x)

35 x = self.CharToNewLineRex.sub("\n",x)

36 x = self.CharToNextTabRex.sub("\t",x)

37 x = self.EndCharToNoneRex.sub("",x)

38

39 for t in self.replaceTab:

40 x = x.replace(t[0],t[1])

41 return x

42

43 class Baidu_Spider:

44 # 申明相关的属性

45 def __init__(self,url):

46 self.myUrl = url + \'?see_lz=1\'

47 self.datas = []

48 self.myTool = HTML_Tool()

49 print u\'已经启动*敏*感*词*爬虫,咔嚓咔嚓\'

50

51 # 初始化加载页面并将其转码储存

52 def baidu_tieba(self):

53 # 读取页面的原始信息并将其从gbk转码

54 myPage = urllib2.urlopen(self.myUrl).read().decode("utf-8")

55 # 计算楼主发布内容一共有多少页

56 endPage = self.page_counter(myPage)

57 # 获取该帖的标题

58 title = self.find_title(myPage)

59 print u\'文章名称:\' + title

60 # 获取最终的数据

61 self.save_data(self.myUrl,title,endPage)

62

63 #用来计算一共有多少页

64 def page_counter(self,myPage):

65 # 匹配 "共有12页" 来获取一共有多少页

66 myMatch = re.search(r\'class="red">(\d+?)\', myPage, re.S)

67 if myMatch:

68 endPage = int(myMatch.group(1))

69 print u\'爬虫报告:发现楼主共有%d页的原创内容\' % endPage

70 else:

71 endPage = 0

72 print u\'爬虫报告:无法计算楼主发布内容有多少页!\'

73 return endPage

74

75 # 用来寻找该帖的标题

76 def find_title(self,myPage):

77 # 匹配 xxxxxxxxxx 找出标题

78 myMatch = re.search(r\'(.*?)\', myPage, re.S)

79 title = u\'暂无标题\'

80 if myMatch:

81 #title = myMatch.group(1)

82 title = myMatch.group(1)

83 else:

84 print u\'爬虫报告:无法加载文章标题!\'

85 # 文件名不能包含以下字符: \ / : * ? " < > |

86 title = title.replace(\'\\\',\'\').replace(\'/\',\'\').replace(\':\',\'\').replace(\'*\',\'\').replace(\'?\',\'\').replace(\'"\',\'\').replace(\'>\',\'\').replace(\'

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线