代码:

# 单帖爬虫,用于爬取理想论坛单个帖子得到发帖人,发帖时间和回帖时间并存入数据库,url例子见main函数
from bs4 import BeautifulSoup
import requests
import threading
import re
import pymysql

user_agent='Mozilla/4.0 (compatible;MEIE 5.5;windows NT)'
headers={'User-Agent':user_agent}

# 帖子爬虫类(多线程)
class topicCrawler(threading.Thread):
    def __init__(self,name,url):
        threading.Thread.__init__(self,name=name)
        self.name=name
        self.url=url
        self.infos=[]

    def run(self):
        while(self.url!="none"):
            print("线程"+self.name+"开始爬取页面"+self.url);

            try:
                rsp=requests.get(self.url,headers=headers)
                self.url="none"#用完之后置空,看下一页能否取到值
                soup= BeautifulSoup(rsp.text,'html.parser',from_encoding='utf-8')
                #print(rsp.text); # rsp.text是全文

                # 找出一页里每条发言
                for divs in soup.find_all('div',class_="postinfo"):
                    #print(divs.text) # divs.text包含作者和发帖时间的文字

                    # 用正则表达式将多个空白字符替换成一个空格
                    RE = re.compile(r'(\s+)')
                    line=RE.sub(" ",divs.text)

                    arr=line.split(' ')

                    #print(len(arr))
                    arrLength=len(arr)

                    if arrLength==7:
                        info={'楼层':arr[1],
                              '作者':arr[2].replace('只看:',''),
                              '日期':arr[4],
                              '时间':arr[5]}
                        self.infos.append(info);
                    elif arrLength==8:
                        info={'楼层':arr[1],
                              '作者':arr[2].replace('只看:',''),
                              '日期':arr[5],
                              '时间':arr[6]}
                        self.infos.append(info);

                #找下一页所在地址
                for pagesDiv in soup.find_all('div',class_="pages"):
                    for strong in pagesDiv.find_all('strong'):
                        print('当前为第'+strong.text+'页')

                        # 找右边的兄弟节点
                        nextNode=strong.next_sibling
                        if nextNode and nextNode.get("href"): # 右边的兄弟节点存在,且其有href属性
                            #print(nextNode.get("href"))
                            self.url='http://www.55188.com/'+nextNode.get("href")

                if self.url!="none":
                    print("有下一页,线程"+self.name+"前往下一页")
                    continue
                else:
                    print("无下一页,线程"+self.name+'爬取结束,开始打印...')

                    for info in self.infos:
                        print('\n')
                        for key in info:
                            print(key+":"+info[key])

                    print("线程"+self.name+'打印结束.')

                    insertDB(self.infos)

            except Exception as e:
                print("线程"+self.name+"发生异常。重新爬行")# 不管怎么出现的异常,就让它一直爬到底
                print(e);
                continue

# 数据库插值
def insertDB(infos):
    conn=pymysql.connect(host=',db='test',charset='utf8')

    for info in infos:
        sql="insert into test.topic(floor,author,tdate,ttime) values ('"+info['楼层']+"','"+info['作者']+"','"+info['日期']+"','"+info['时间']+"'"+")"
        print(sql)
        conn.query(sql)

    conn.commit()# 写操作之后commit不可少
    conn.close()

# 入口函数
def main():
        #http://www.55188.com/thread-8205979-1-1.html
        #http://www.55188.com/thread-8324517-1-1.html
        #http://www.55188.com/thread-8205979-61-1.html
        url='http://www.55188.com/thread-8319519-1-1.html'
        tc=topicCrawler(name='crawler01',url=url)
        tc.start()

# 开始
main()

输出:

C:\Users\horn1\Desktop\python\15>python topicCrawler.py
线程crawler01开始爬取页面http://www.55188.com/thread-8319519-1-1.html
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.
  warnings.warn("You provided Unicode markup but also provided a value for from_encoding. Your from_encoding will be ignored.")
当前为第1页
当前为第1页
有下一页,线程crawler01前往下一页
线程crawler01开始爬取页面http://www.55188.com/thread-8319519-2-1.html
当前为第2页
当前为第2页
有下一页,线程crawler01前往下一页
线程crawler01开始爬取页面http://www.55188.com/thread-8319519-3-1.html
当前为第3页
当前为第3页
无下一页,线程crawler01爬取结束,开始打印...

楼层:楼主
作者:马泰的哥们
日期:2018-3-30
时间:09:59

楼层:2楼
作者:龙波2010
日期:2018-3-30
时间:10:00

楼层:3楼
作者:吗日个边
日期:2018-3-30
时间:10:07

楼层:4楼
作者:小兵旨
日期:2018-3-30
时间:10:30

楼层:5楼
作者:勇儿马甲
日期:2018-3-30
时间:10:37

楼层:6楼
作者:培训资料
日期:2018-3-30
时间:10:43

楼层:7楼
作者:短线冲
日期:2018-3-30
时间:10:56

楼层:8楼
作者:马泰的哥们
日期:2018-3-30
时间:10:59

楼层:9楼
作者:一赚
日期:2018-3-30
时间:11:01

楼层:10楼
作者:叼叼狼
日期:2018-3-30
时间:11:25

楼层:11楼
作者:酷我行
日期:2018-3-30
时间:11:40

楼层:12楼
作者:马泰的哥们
日期:2018-3-30
时间:11:48

楼层:13楼
作者:马泰的哥们
日期:2018-3-30
时间:11:49

楼层:14楼
作者:生活如愿
日期:2018-3-30
时间:11:55

楼层:15楼
作者:小兵旨
日期:2018-3-30
时间:12:42

楼层:16楼
作者:李汶安
日期:2018-3-30
时间:12:50

楼层:17楼
作者:马泰的哥们
日期:2018-3-30
时间:13:46

楼层:18楼
作者:小兵旨
日期:2018-3-30
时间:13:49

楼层:19楼
作者:马泰的哥们
日期:2018-3-30
时间:14:02

楼层:20楼
作者:酷我行
日期:2018-3-30
时间:17:21

楼层:21楼
作者:酷我行
日期:2018-3-30
时间:17:24

楼层:22楼
作者:马泰的哥们
日期:2018-3-30
时间:17:30

楼层:23楼
作者:酷我行
日期:2018-3-30
时间:21:37

楼层:24楼
作者:马泰的哥们
日期:2018-3-30
时间:21:44

楼层:25楼
作者:破局
日期:2018-3-30
时间:21:50

楼层:26楼
作者:小中大学生
日期:2018-3-31
时间:00:27

楼层:27楼
作者:理想5e9a18
日期:2018-3-31
时间:00:57

楼层:28楼
作者:龍樹
日期:2018-3-31
时间:06:29

楼层:29楼
作者:生活如愿
日期:2018-3-31
时间:07:49

楼层:30楼
作者:胶东判官
日期:2018-3-31
时间:12:32

楼层:31楼
作者:胶东判官
日期:2018-3-31
时间:12:32

楼层:32楼
作者:天上下鱼
日期:2018-3-31
时间:13:04

楼层:33楼
作者:天上下鱼
日期:2018-3-31
时间:13:05

楼层:34楼
作者:股市小小手
日期:2018-3-31
时间:14:48

楼层:35楼
作者:股市小小手
日期:2018-3-31
时间:14:50

楼层:36楼
作者:逍遥茶
日期:2018-3-31
时间:15:45

楼层:37楼
作者:马泰的哥们
日期:2018-4-1
时间:03:01

楼层:38楼
作者:理想5e9a18
日期:2018-4-1
时间:03:04

楼层:39楼
作者:马泰的哥们
日期:2018-4-1
时间:03:05

楼层:40楼
作者:陈龙333
日期:2018-4-1
时间:03:05

楼层:41楼
作者:马泰的哥们
日期:2018-4-1
时间:03:08

楼层:42楼
作者:理想5e9a18
日期:2018-4-1
时间:03:10

楼层:43楼
作者:马泰的哥们
日期:2018-4-2
时间:09:30

楼层:44楼
作者:理想5e9a18
日期:2018-4-2
时间:11:18

楼层:45楼
作者:马泰效应
日期:2018-4-4
时间:03:00

楼层:46楼
作者:马泰效应
日期:2018-4-4
时间:03:00

楼层:47楼
作者:韭菜008
日期:2018-4-4
时间:08:08
线程crawler01打印结束.
insert into test.topic(floor,author,tdate,ttime) values ('楼主','马泰的哥们','2018-3-30','09:59')
insert into test.topic(floor,author,tdate,ttime) values ('2楼','龙波2010','2018-3-30','10:00')
insert into test.topic(floor,author,tdate,ttime) values ('3楼','吗日个边','2018-3-30','10:07')
insert into test.topic(floor,author,tdate,ttime) values ('4楼','小兵旨','2018-3-30','10:30')
insert into test.topic(floor,author,tdate,ttime) values ('5楼','勇儿马甲','2018-3-30','10:37')
insert into test.topic(floor,author,tdate,ttime) values ('6楼','培训资料','2018-3-30','10:43')
insert into test.topic(floor,author,tdate,ttime) values ('7楼','短线冲','2018-3-30','10:56')
insert into test.topic(floor,author,tdate,ttime) values ('8楼','马泰的哥们','2018-3-30','10:59')
insert into test.topic(floor,author,tdate,ttime) values ('9楼','一赚','2018-3-30','11:01')
insert into test.topic(floor,author,tdate,ttime) values ('10楼','叼叼狼','2018-3-30','11:25')
insert into test.topic(floor,author,tdate,ttime) values ('11楼','酷我行','2018-3-30','11:40')
insert into test.topic(floor,author,tdate,ttime) values ('12楼','马泰的哥们','2018-3-30','11:48')
insert into test.topic(floor,author,tdate,ttime) values ('13楼','马泰的哥们','2018-3-30','11:49')
insert into test.topic(floor,author,tdate,ttime) values ('14楼','生活如愿','2018-3-30','11:55')
insert into test.topic(floor,author,tdate,ttime) values ('15楼','小兵旨','2018-3-30','12:42')
insert into test.topic(floor,author,tdate,ttime) values ('16楼','李汶安','2018-3-30','12:50')
insert into test.topic(floor,author,tdate,ttime) values ('17楼','马泰的哥们','2018-3-30','13:46')
insert into test.topic(floor,author,tdate,ttime) values ('18楼','小兵旨','2018-3-30','13:49')
insert into test.topic(floor,author,tdate,ttime) values ('19楼','马泰的哥们','2018-3-30','14:02')
insert into test.topic(floor,author,tdate,ttime) values ('20楼','酷我行','2018-3-30','17:21')
insert into test.topic(floor,author,tdate,ttime) values ('21楼','酷我行','2018-3-30','17:24')
insert into test.topic(floor,author,tdate,ttime) values ('22楼','马泰的哥们','2018-3-30','17:30')
insert into test.topic(floor,author,tdate,ttime) values ('23楼','酷我行','2018-3-30','21:37')
insert into test.topic(floor,author,tdate,ttime) values ('24楼','马泰的哥们','2018-3-30','21:44')
insert into test.topic(floor,author,tdate,ttime) values ('25楼','破局','2018-3-30','21:50')
insert into test.topic(floor,author,tdate,ttime) values ('26楼','小中大学生','2018-3-31','00:27')
insert into test.topic(floor,author,tdate,ttime) values ('27楼','理想5e9a18','2018-3-31','00:57')
insert into test.topic(floor,author,tdate,ttime) values ('28楼','龍樹','2018-3-31','06:29')
insert into test.topic(floor,author,tdate,ttime) values ('29楼','生活如愿','2018-3-31','07:49')
insert into test.topic(floor,author,tdate,ttime) values ('30楼','胶东判官','2018-3-31','12:32')
insert into test.topic(floor,author,tdate,ttime) values ('31楼','胶东判官','2018-3-31','12:32')
insert into test.topic(floor,author,tdate,ttime) values ('32楼','天上下鱼','2018-3-31','13:04')
insert into test.topic(floor,author,tdate,ttime) values ('33楼','天上下鱼','2018-3-31','13:05')
insert into test.topic(floor,author,tdate,ttime) values ('34楼','股市小小手','2018-3-31','14:48')
insert into test.topic(floor,author,tdate,ttime) values ('35楼','股市小小手','2018-3-31','14:50')
insert into test.topic(floor,author,tdate,ttime) values ('36楼','逍遥茶','2018-3-31','15:45')
insert into test.topic(floor,author,tdate,ttime) values ('37楼','马泰的哥们','2018-4-1','03:01')
insert into test.topic(floor,author,tdate,ttime) values ('38楼','理想5e9a18','2018-4-1','03:04')
insert into test.topic(floor,author,tdate,ttime) values ('39楼','马泰的哥们','2018-4-1','03:05')
insert into test.topic(floor,author,tdate,ttime) values ('40楼','陈龙333','2018-4-1','03:05')
insert into test.topic(floor,author,tdate,ttime) values ('41楼','马泰的哥们','2018-4-1','03:08')
insert into test.topic(floor,author,tdate,ttime) values ('42楼','理想5e9a18','2018-4-1','03:10')
insert into test.topic(floor,author,tdate,ttime) values ('43楼','马泰的哥们','2018-4-2','09:30')
insert into test.topic(floor,author,tdate,ttime) values ('44楼','理想5e9a18','2018-4-2','11:18')
insert into test.topic(floor,author,tdate,ttime) values ('45楼','马泰效应','2018-4-4','03:00')
insert into test.topic(floor,author,tdate,ttime) values ('46楼','马泰效应','2018-4-4','03:00')
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. cinder 挂载卷和 iSCSI原理

    LVM名称介绍 PV:物理磁盘 VG:卷组,多个PV可以组成一个VG,一个VG可以划分成多个LV(逻辑卷). PP:物理区块,将一个VG逻辑的划分成连续的小块. LP:逻辑区块,若干个PP组成一个LP ...

  2. BZOJ1030 [JSOI2007]文本生成器(AC自动机)

    做到了AC自动机的题目,复习了一下AC自动机,学习了黄学长代码,这个题呢,我们可以模拟在AC自动机上的操作,dp数组f[i][j]表示前i个字符,我们在AC自动机上处在j号节点的方案数. 我们可以计算 ...

  3. 【tarjan+拓扑】BZOJ3887-[Usaco2015 Jan]Grass Cownoisseur

    [题目大意] 给一个有向图,然后选一条路径起点终点都为1的路径出来,有一次机会可以沿某条边逆方向走,问最多有多少个点可以被经过?(一个点在路径中无论出现多少正整数次对答案的贡献均为1) [思路] 首先 ...

  4. Arab Collegiate Programming Contest 2012 J- Math Homework

    思路:由于只有1-6这几个数,而这几个数的最小公倍数是60,所以只需要求出60以内有多少满足条件的数即可. 再就是求出对于给定的n,求出60的倍数.然后就是怎样求的问题了. 首先可以写成如下形式:   ...

  5. Unity UGUI之Text

    下图是Text组件的内容. Character(字符) Text--输入要显示的文本 Font--要渲染文本的字体类型(例如:黑体.宋体) FontStyle--是否要加粗,倾斜等. Normal-- ...

  6. css背景图片模糊

    index.html <image class="bg" src="/images/bg.png"></image> index.css ...

  7. Caused by: java.net.UnknownHostException: localhost.localdomain: localhost.localdomain的问题解决

    在hosts文件增加如下配置即可,下面的方法适合上面提示的错误,无论是Tomcat问题还是MongoDB等等的问题都可以完美解决. vi /etc/hosts 127.0.0.1 localhost ...

  8. [IIS]由安装IIS和.net framework先后顺序引发的问题,你中招了吗?

    引言 最近帮别人做了一个小网站,在本机部署测试的时候,竟然浏览不了aspx后缀的页面,但可以浏览html页面,由此想到了IIS对静态页和动态页不同的处理方式. http请求到达服务器 当服务器接收到一 ...

  9. C#中有关资源、BeginInvoke, Invoke和事件的事情

    事情是这么来的,我开发的一个程序报了一个错误 “在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke错误”. 然后我在网上查资料,发现一个有意思的问题,文章出处为“在创建窗口 ...

  10. ArcGIS for Android地图上实际距离与对应的屏幕像素值计算

    本篇文章主要介绍了"ArcGIS for Android地图上实际距离与对应的屏幕像素值计算",主要涉及到ArcGIS for Android地图上实际距离与对应的屏幕像素值计算方 ...