代码:

  1. # 单帖爬虫,用于爬取理想论坛单个帖子得到发帖人,发帖时间和回帖时间并存入数据库,url例子见main函数
  2. from bs4 import BeautifulSoup
  3. import requests
  4. import threading
  5. import re
  6. import pymysql
  7.  
  8. user_agent='Mozilla/4.0 (compatible;MEIE 5.5;windows NT)'
  9. headers={'User-Agent':user_agent}
  10.  
  11. # 帖子爬虫类(多线程)
  12. class topicCrawler(threading.Thread):
  13. def __init__(self,name,url):
  14. threading.Thread.__init__(self,name=name)
  15. self.name=name
  16. self.url=url
  17. self.infos=[]
  18.  
  19. def run(self):
  20. while(self.url!="none"):
  21. print("线程"+self.name+"开始爬取页面"+self.url);
  22.  
  23. try:
  24. rsp=requests.get(self.url,headers=headers)
  25. self.url="none"#用完之后置空,看下一页能否取到值
  26. soup= BeautifulSoup(rsp.text,'html.parser',from_encoding='utf-8')
  27. #print(rsp.text); # rsp.text是全文
  28.  
  29. # 找出一页里每条发言
  30. for divs in soup.find_all('div',class_="postinfo"):
  31. #print(divs.text) # divs.text包含作者和发帖时间的文字
  32.  
  33. # 用正则表达式将多个空白字符替换成一个空格
  34. RE = re.compile(r'(\s+)')
  35. line=RE.sub(" ",divs.text)
  36.  
  37. arr=line.split(' ')
  38.  
  39. #print(len(arr))
  40. arrLength=len(arr)
  41.  
  42. if arrLength==7:
  43. info={'楼层':arr[1],
  44. '作者':arr[2].replace('只看:',''),
  45. '日期':arr[4],
  46. '时间':arr[5]}
  47. self.infos.append(info);
  48. elif arrLength==8:
  49. info={'楼层':arr[1],
  50. '作者':arr[2].replace('只看:',''),
  51. '日期':arr[5],
  52. '时间':arr[6]}
  53. self.infos.append(info);
  54.  
  55. #找下一页所在地址
  56. for pagesDiv in soup.find_all('div',class_="pages"):
  57. for strong in pagesDiv.find_all('strong'):
  58. print('当前为第'+strong.text+'页')
  59.  
  60. # 找右边的兄弟节点
  61. nextNode=strong.next_sibling
  62. if nextNode and nextNode.get("href"): # 右边的兄弟节点存在,且其有href属性
  63. #print(nextNode.get("href"))
  64. self.url='http://www.55188.com/'+nextNode.get("href")
  65.  
  66. if self.url!="none":
  67. print("有下一页,线程"+self.name+"前往下一页")
  68. continue
  69. else:
  70. print("无下一页,线程"+self.name+'爬取结束,开始打印...')
  71.  
  72. for info in self.infos:
  73. print('\n')
  74. for key in info:
  75. print(key+":"+info[key])
  76.  
  77. print("线程"+self.name+'打印结束.')
  78.  
  79. insertDB(self.infos)
  80.  
  81. except Exception as e:
  82. print("线程"+self.name+"发生异常。重新爬行")# 不管怎么出现的异常,就让它一直爬到底
  83. print(e);
  84. continue
  85.  
  86. # 数据库插值
  87. def insertDB(infos):
  88. conn=pymysql.connect(host=',db='test',charset='utf8')
  89.  
  90. for info in infos:
  91. sql="insert into test.topic(floor,author,tdate,ttime) values ('"+info['楼层']+"','"+info['作者']+"','"+info['日期']+"','"+info['时间']+"'"+")"
  92. print(sql)
  93. conn.query(sql)
  94.  
  95. conn.commit()# 写操作之后commit不可少
  96. conn.close()
  97.  
  98. # 入口函数
  99. def main():
  100. #http://www.55188.com/thread-8205979-1-1.html
  101. #http://www.55188.com/thread-8324517-1-1.html
  102. #http://www.55188.com/thread-8205979-61-1.html
  103. url='http://www.55188.com/thread-8319519-1-1.html'
  104. tc=topicCrawler(name='crawler01',url=url)
  105. tc.start()
  106.  
  107. # 开始
  108. main()

输出:

  1. C:\Users\horn1\Desktop\python\15>python topicCrawler.py
  2. 线程crawler01开始爬取页面http://www.55188.com/thread-8319519-1-1.html
  3. C:\Users\horn1\AppData\Local\Programs\Python\Python36\lib\site-packages\bs4\__init__.py:146: UserWarning: You provided Unicode markup but also provided a value for from_encoding. Your from_encoding will be ignored.
  4. warnings.warn("You provided Unicode markup but also provided a value for from_encoding. Your from_encoding will be ignored.")
  5. 当前为第1
  6. 当前为第1
  7. 有下一页,线程crawler01前往下一页
  8. 线程crawler01开始爬取页面http://www.55188.com/thread-8319519-2-1.html
  9. 当前为第2
  10. 当前为第2
  11. 有下一页,线程crawler01前往下一页
  12. 线程crawler01开始爬取页面http://www.55188.com/thread-8319519-3-1.html
  13. 当前为第3
  14. 当前为第3
  15. 无下一页,线程crawler01爬取结束,开始打印...
  16.  
  17. 楼层:楼主
  18. 作者:马泰的哥们
  19. 日期:2018-3-30
  20. 时间:09:59
  21.  
  22. 楼层:2
  23. 作者:龙波2010
  24. 日期:2018-3-30
  25. 时间:10:00
  26.  
  27. 楼层:3
  28. 作者:吗日个边
  29. 日期:2018-3-30
  30. 时间:10:07
  31.  
  32. 楼层:4
  33. 作者:小兵旨
  34. 日期:2018-3-30
  35. 时间:10:30
  36.  
  37. 楼层:5
  38. 作者:勇儿马甲
  39. 日期:2018-3-30
  40. 时间:10:37
  41.  
  42. 楼层:6
  43. 作者:培训资料
  44. 日期:2018-3-30
  45. 时间:10:43
  46.  
  47. 楼层:7
  48. 作者:短线冲
  49. 日期:2018-3-30
  50. 时间:10:56
  51.  
  52. 楼层:8
  53. 作者:马泰的哥们
  54. 日期:2018-3-30
  55. 时间:10:59
  56.  
  57. 楼层:9
  58. 作者:一赚
  59. 日期:2018-3-30
  60. 时间:11:01
  61.  
  62. 楼层:10
  63. 作者:叼叼狼
  64. 日期:2018-3-30
  65. 时间:11:25
  66.  
  67. 楼层:11
  68. 作者:酷我行
  69. 日期:2018-3-30
  70. 时间:11:40
  71.  
  72. 楼层:12
  73. 作者:马泰的哥们
  74. 日期:2018-3-30
  75. 时间:11:48
  76.  
  77. 楼层:13
  78. 作者:马泰的哥们
  79. 日期:2018-3-30
  80. 时间:11:49
  81.  
  82. 楼层:14
  83. 作者:生活如愿
  84. 日期:2018-3-30
  85. 时间:11:55
  86.  
  87. 楼层:15
  88. 作者:小兵旨
  89. 日期:2018-3-30
  90. 时间:12:42
  91.  
  92. 楼层:16
  93. 作者:李汶安
  94. 日期:2018-3-30
  95. 时间:12:50
  96.  
  97. 楼层:17
  98. 作者:马泰的哥们
  99. 日期:2018-3-30
  100. 时间:13:46
  101.  
  102. 楼层:18
  103. 作者:小兵旨
  104. 日期:2018-3-30
  105. 时间:13:49
  106.  
  107. 楼层:19
  108. 作者:马泰的哥们
  109. 日期:2018-3-30
  110. 时间:14:02
  111.  
  112. 楼层:20
  113. 作者:酷我行
  114. 日期:2018-3-30
  115. 时间:17:21
  116.  
  117. 楼层:21
  118. 作者:酷我行
  119. 日期:2018-3-30
  120. 时间:17:24
  121.  
  122. 楼层:22
  123. 作者:马泰的哥们
  124. 日期:2018-3-30
  125. 时间:17:30
  126.  
  127. 楼层:23
  128. 作者:酷我行
  129. 日期:2018-3-30
  130. 时间:21:37
  131.  
  132. 楼层:24
  133. 作者:马泰的哥们
  134. 日期:2018-3-30
  135. 时间:21:44
  136.  
  137. 楼层:25
  138. 作者:破局
  139. 日期:2018-3-30
  140. 时间:21:50
  141.  
  142. 楼层:26
  143. 作者:小中大学生
  144. 日期:2018-3-31
  145. 时间:00:27
  146.  
  147. 楼层:27
  148. 作者:理想5e9a18
  149. 日期:2018-3-31
  150. 时间:00:57
  151.  
  152. 楼层:28
  153. 作者:龍樹
  154. 日期:2018-3-31
  155. 时间:06:29
  156.  
  157. 楼层:29
  158. 作者:生活如愿
  159. 日期:2018-3-31
  160. 时间:07:49
  161.  
  162. 楼层:30
  163. 作者:胶东判官
  164. 日期:2018-3-31
  165. 时间:12:32
  166.  
  167. 楼层:31
  168. 作者:胶东判官
  169. 日期:2018-3-31
  170. 时间:12:32
  171.  
  172. 楼层:32
  173. 作者:天上下鱼
  174. 日期:2018-3-31
  175. 时间:13:04
  176.  
  177. 楼层:33
  178. 作者:天上下鱼
  179. 日期:2018-3-31
  180. 时间:13:05
  181.  
  182. 楼层:34
  183. 作者:股市小小手
  184. 日期:2018-3-31
  185. 时间:14:48
  186.  
  187. 楼层:35
  188. 作者:股市小小手
  189. 日期:2018-3-31
  190. 时间:14:50
  191.  
  192. 楼层:36
  193. 作者:逍遥茶
  194. 日期:2018-3-31
  195. 时间:15:45
  196.  
  197. 楼层:37
  198. 作者:马泰的哥们
  199. 日期:2018-4-1
  200. 时间:03:01
  201.  
  202. 楼层:38
  203. 作者:理想5e9a18
  204. 日期:2018-4-1
  205. 时间:03:04
  206.  
  207. 楼层:39
  208. 作者:马泰的哥们
  209. 日期:2018-4-1
  210. 时间:03:05
  211.  
  212. 楼层:40
  213. 作者:陈龙333
  214. 日期:2018-4-1
  215. 时间:03:05
  216.  
  217. 楼层:41
  218. 作者:马泰的哥们
  219. 日期:2018-4-1
  220. 时间:03:08
  221.  
  222. 楼层:42
  223. 作者:理想5e9a18
  224. 日期:2018-4-1
  225. 时间:03:10
  226.  
  227. 楼层:43
  228. 作者:马泰的哥们
  229. 日期:2018-4-2
  230. 时间:09:30
  231.  
  232. 楼层:44
  233. 作者:理想5e9a18
  234. 日期:2018-4-2
  235. 时间:11:18
  236.  
  237. 楼层:45
  238. 作者:马泰效应
  239. 日期:2018-4-4
  240. 时间:03:00
  241.  
  242. 楼层:46
  243. 作者:马泰效应
  244. 日期:2018-4-4
  245. 时间:03:00
  246.  
  247. 楼层:47
  248. 作者:韭菜008
  249. 日期:2018-4-4
  250. 时间:08:08
  251. 线程crawler01打印结束.
  252. insert into test.topic(floor,author,tdate,ttime) values ('楼主','马泰的哥们','2018-3-30','09:59')
  253. insert into test.topic(floor,author,tdate,ttime) values ('2楼','龙波2010','2018-3-30','10:00')
  254. insert into test.topic(floor,author,tdate,ttime) values ('3楼','吗日个边','2018-3-30','10:07')
  255. insert into test.topic(floor,author,tdate,ttime) values ('4楼','小兵旨','2018-3-30','10:30')
  256. insert into test.topic(floor,author,tdate,ttime) values ('5楼','勇儿马甲','2018-3-30','10:37')
  257. insert into test.topic(floor,author,tdate,ttime) values ('6楼','培训资料','2018-3-30','10:43')
  258. insert into test.topic(floor,author,tdate,ttime) values ('7楼','短线冲','2018-3-30','10:56')
  259. insert into test.topic(floor,author,tdate,ttime) values ('8楼','马泰的哥们','2018-3-30','10:59')
  260. insert into test.topic(floor,author,tdate,ttime) values ('9楼','一赚','2018-3-30','11:01')
  261. insert into test.topic(floor,author,tdate,ttime) values ('10楼','叼叼狼','2018-3-30','11:25')
  262. insert into test.topic(floor,author,tdate,ttime) values ('11楼','酷我行','2018-3-30','11:40')
  263. insert into test.topic(floor,author,tdate,ttime) values ('12楼','马泰的哥们','2018-3-30','11:48')
  264. insert into test.topic(floor,author,tdate,ttime) values ('13楼','马泰的哥们','2018-3-30','11:49')
  265. insert into test.topic(floor,author,tdate,ttime) values ('14楼','生活如愿','2018-3-30','11:55')
  266. insert into test.topic(floor,author,tdate,ttime) values ('15楼','小兵旨','2018-3-30','12:42')
  267. insert into test.topic(floor,author,tdate,ttime) values ('16楼','李汶安','2018-3-30','12:50')
  268. insert into test.topic(floor,author,tdate,ttime) values ('17楼','马泰的哥们','2018-3-30','13:46')
  269. insert into test.topic(floor,author,tdate,ttime) values ('18楼','小兵旨','2018-3-30','13:49')
  270. insert into test.topic(floor,author,tdate,ttime) values ('19楼','马泰的哥们','2018-3-30','14:02')
  271. insert into test.topic(floor,author,tdate,ttime) values ('20楼','酷我行','2018-3-30','17:21')
  272. insert into test.topic(floor,author,tdate,ttime) values ('21楼','酷我行','2018-3-30','17:24')
  273. insert into test.topic(floor,author,tdate,ttime) values ('22楼','马泰的哥们','2018-3-30','17:30')
  274. insert into test.topic(floor,author,tdate,ttime) values ('23楼','酷我行','2018-3-30','21:37')
  275. insert into test.topic(floor,author,tdate,ttime) values ('24楼','马泰的哥们','2018-3-30','21:44')
  276. insert into test.topic(floor,author,tdate,ttime) values ('25楼','破局','2018-3-30','21:50')
  277. insert into test.topic(floor,author,tdate,ttime) values ('26楼','小中大学生','2018-3-31','00:27')
  278. insert into test.topic(floor,author,tdate,ttime) values ('27楼','理想5e9a18','2018-3-31','00:57')
  279. insert into test.topic(floor,author,tdate,ttime) values ('28楼','龍樹','2018-3-31','06:29')
  280. insert into test.topic(floor,author,tdate,ttime) values ('29楼','生活如愿','2018-3-31','07:49')
  281. insert into test.topic(floor,author,tdate,ttime) values ('30楼','胶东判官','2018-3-31','12:32')
  282. insert into test.topic(floor,author,tdate,ttime) values ('31楼','胶东判官','2018-3-31','12:32')
  283. insert into test.topic(floor,author,tdate,ttime) values ('32楼','天上下鱼','2018-3-31','13:04')
  284. insert into test.topic(floor,author,tdate,ttime) values ('33楼','天上下鱼','2018-3-31','13:05')
  285. insert into test.topic(floor,author,tdate,ttime) values ('34楼','股市小小手','2018-3-31','14:48')
  286. insert into test.topic(floor,author,tdate,ttime) values ('35楼','股市小小手','2018-3-31','14:50')
  287. insert into test.topic(floor,author,tdate,ttime) values ('36楼','逍遥茶','2018-3-31','15:45')
  288. insert into test.topic(floor,author,tdate,ttime) values ('37楼','马泰的哥们','2018-4-1','03:01')
  289. insert into test.topic(floor,author,tdate,ttime) values ('38楼','理想5e9a18','2018-4-1','03:04')
  290. insert into test.topic(floor,author,tdate,ttime) values ('39楼','马泰的哥们','2018-4-1','03:05')
  291. insert into test.topic(floor,author,tdate,ttime) values ('40楼','陈龙333','2018-4-1','03:05')
  292. insert into test.topic(floor,author,tdate,ttime) values ('41楼','马泰的哥们','2018-4-1','03:08')
  293. insert into test.topic(floor,author,tdate,ttime) values ('42楼','理想5e9a18','2018-4-1','03:10')
  294. insert into test.topic(floor,author,tdate,ttime) values ('43楼','马泰的哥们','2018-4-2','09:30')
  295. insert into test.topic(floor,author,tdate,ttime) values ('44楼','理想5e9a18','2018-4-2','11:18')
  296. insert into test.topic(floor,author,tdate,ttime) values ('45楼','马泰效应','2018-4-4','03:00')
  297. insert into test.topic(floor,author,tdate,ttime) values ('46楼','马泰效应','2018-4-4','03:00')
  298. insert into test.topic(floor,author,tdate,ttime) values ('47楼','韭菜008','2018-4-4','08:08')

数据库中数据:

【pyhon】理想论坛单帖爬虫取得信息存入MySql数据库的更多相关文章

  1. 【Python】爬取理想论坛单帖爬虫

    代码: # 单帖爬虫,用于爬取理想论坛帖子得到发帖人,发帖时间和回帖时间,url例子见main函数 from bs4 import BeautifulSoup import requests impo ...

  2. 【nodejs】理想论坛帖子下载爬虫1.08

    //====================================================== // 理想论坛帖子下载爬虫1.09 // 使用断点续传模式,因为网络传输会因各种原因中 ...

  3. 【nodejs】理想论坛帖子下载爬虫1.07 使用request模块后稳定多了

    在1.06版本时,访问网页采用的时http.request,但调用次数多以后就问题来了. 寻找别的方案时看到了https://cnodejs.org/topic/53142ef833dbcb076d0 ...

  4. 【nodejs】理想论坛帖子下载爬虫1.06

    //====================================================== // 理想论坛帖子下载爬虫1.06 // 循环改成了递归,但最多下载千余文件就崩了 / ...

  5. 【Nodejs】理想论坛帖子下载爬虫1.04

    一直想做一个能把理想论坛指定页范围的帖子都能完整下载下来的爬虫,但未能如愿. 主要的障碍在并发数的控制和长时间任务的突然退出,比如想下载前五页的帖子,分析后可得到大约15000个主贴或子贴,如果用回调 ...

  6. 【Python】理想论坛帖子读取爬虫1.04版

    1.01-1.03版本都有多线程争抢DB的问题,线程数一多问题就严重了. 这个版本把各线程要添加数据的SQL放到数组里,等最后一次性完成,这样就好些了.但乱码问题和未全部完成即退出现象还在,而且速度上 ...

  7. Python scrapy爬虫数据保存到MySQL数据库

    除将爬取到的信息写入文件中之外,程序也可通过修改 Pipeline 文件将数据保存到数据库中.为了使用数据库来保存爬取到的信息,在 MySQL 的 python 数据库中执行如下 SQL 语句来创建 ...

  8. Hive学习之路 (三)Hive元数据信息对应MySQL数据库表

    概述 Hive 的元数据信息通常存储在关系型数据库中,常用MySQL数据库作为元数据库管理.上一篇hive的安装也是将元数据信息存放在MySQL数据库中. Hive的元数据信息在MySQL数据中有57 ...

  9. Hive(三)Hive元数据信息对应MySQL数据库表

    概述 Hive 的元数据信息通常存储在关系型数据库中,常用MySQL数据库作为元数据库管理.上一篇hive的安装也是将元数据信息存放在MySQL数据库中. Hive的元数据信息在MySQL数据中有57 ...

随机推荐

  1. ACID数据库事务正确执行的四个基本要素的缩写

    ACID,指数据库事务正确执行的四个基本要素的缩写.包含:原子性(Atomicity).一致性(Consistency).隔离性(Isolation).持久性(Durability).一个支持事务(T ...

  2. Scrapy基础------css选择器基础

    基本语法: * 选择所有节点 #container 选择id为container的节点 .container 选择所有class包含container的节点 li a 选取所有li 下所有a节点 ul ...

  3. Spring 什么是 IOC 控制反转 ?什么是依赖注入?spring的用处 好处 为什么要用

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha Spring是一个开源的控制反转(Inversion of Control ,IoC)和 ...

  4. 关于那些oj链接

    luogu codeforces bzoj poj tyvj

  5. python开发_os.path

    在python中,os.path模块在处理路径的时候非常有用 下面是我做的demo 运行效果: ========================================= 代码部分: ==== ...

  6. Codeforces Beta Round #4 (Div. 2 Only) B. Before an Exam dp

    B. Before an Exam 题目连接: http://www.codeforces.com/contest/4/problem/B Description Tomorrow Peter has ...

  7. HDU 5643 King's Game 打表

    King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...

  8. 2015 UESTC 搜索专题C题 基爷与加法等式 爆搜DFS

    基爷与加法等式 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Desc ...

  9. python 用gensim进行文本相似度分析

    http://blog.csdn.net/chencheng126/article/details/50070021 参考于这个博主的博文. 原理 1.文本相似度计算的需求始于搜索引擎. 搜索引擎需要 ...

  10. Java代码优化方案 J2ME内存优化

    Java代码优化方案 J2ME内存优化 从几本书上,N个网站上整理的一些JAVA代码优化方案,最近的项目只有1M内存可用,必须很抠门了~J2ME项目更要注意的 避免内存溢出 l 不用的对象释放(置空) ...