代码:

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

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('楼层='+arr[1])
                    #print('作者='+arr[2].replace('只看:',''))
                    #print('日期='+arr[4])
                    #print('时间='+arr[5])
                    info={'楼层':arr[1],
                          '作者':arr[2].replace('只看:',''),
                          '日期':arr[4],
                          '时间':arr[5]}
                    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+'打印结束.')

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

# 入口函数
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\14>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

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

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

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

楼层:12楼
作者:马泰的哥们
日期:发表于
时间:2018-3-30

楼层:13楼
作者:马泰的哥们
日期:发表于
时间:2018-3-30

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

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

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

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

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

楼层:19楼
作者:马泰的哥们
日期:发表于
时间:2018-3-30

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

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

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

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

楼层:24楼
作者:马泰的哥们
日期:发表于
时间:2018-3-30

楼层: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

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

楼层:39楼
作者:马泰的哥们
日期:发表于
时间:2018-4-1

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

楼层:41楼
作者:马泰的哥们
日期:发表于
时间:2018-4-1

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

楼层:43楼
作者:马泰的哥们
日期:发表于
时间:2018-4-2

楼层: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打印结束.

这个爬虫虽然简单,却是大计划中的一步。

【Python】爬取理想论坛单帖爬虫的更多相关文章

  1. 【pyhon】理想论坛单帖爬虫取得信息存入MySql数据库

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

  2. Java爬取校内论坛新帖

    Java爬取校内论坛新帖 为了保持消息灵通,博主没事会上上校内论坛看看新帖,作为爬虫爱好者,博主萌生了写个爬虫自动下载的想法. 嗯,这次就选Java. 第三方库准备 Jsoup Jsoup是一款比较好 ...

  3. 【ichartjs】爬取理想论坛前30页帖子获得每个子贴的发帖时间,总计83767条数据进行统计,生成统计图表

    统计数据如下: {': 2451} 图形化后效果如下: 源码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//E ...

  4. 【Python爬虫案例学习】Python爬取天涯论坛评论

    用到的包有requests - BeautSoup 我爬的是天涯论坛的财经论坛:'http://bbs.tianya.cn/list.jsp?item=develop' 它里面的其中的一个帖子的URL ...

  5. python 爬取猫眼榜单100(二)--多个页面以及多进程

    #!/usr/bin/env python # -*- coding: utf- -*- # @Author: Dang Kai # @Date: -- :: # @Last Modified tim ...

  6. [Python]爬取CSDN论坛 标题 2020.2.8

    首先新建一个Lei.txt 内容为: CloudComputingParentBlockchainTechnologyEnterpriseDotNETJavaWebDevelopVCVBDelphiB ...

  7. Python爬取网易云音乐歌手歌曲和歌单

    仅供学习参考 Python爬取网易云音乐网易云音乐歌手歌曲和歌单,并下载到本地 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做 ...

  8. 【Python爬虫案例】用Python爬取李子柒B站视频数据

    一.视频数据结果 今天是2021.12.7号,前几天用python爬取了李子柒的油管评论并做了数据分析,可移步至: https://www.cnblogs.com/mashukui/p/1622025 ...

  9. Python爬取CSDN博客文章

    0 url :http://blog.csdn.net/youyou1543724847/article/details/52818339Redis一点基础的东西目录 1.基础底层数据结构 2.win ...

随机推荐

  1. 深度学习笔记之【随机梯度下降(SGD)】

    随机梯度下降 几乎所有的深度学习算法都用到了一个非常重要的算法:随机梯度下降(stochastic gradient descent,SGD) 随机梯度下降是梯度下降算法的一个扩展 机器学习中一个反复 ...

  2. SpringMVC框架 之 from标签(转)

    原文地址:http://blog.csdn.net/kutim/article/details/46682547 spring表单标签   <%@taglib uri="http:// ...

  3. y450 archlinux cuda6.5

    y450 archlinux cuda6.5 January 28, 2018 4:11 PM archlinux是最新更新版本,gcc版本到了7.几,太新了. [qiangge@lqspc ~]$ ...

  4. nyoj 18 The Triangle

    The Triangle 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure ...

  5. django验证码配置与使用

    1.安装django-simple-captcha pip install django-simple-captcha 2.配置settings.py ##加app列表INSTALLED_APPS = ...

  6. UVALive 6907 Body Building

    题目链接:https://vjudge.net/problem/UVALive-6907 题意: 给出一张图,判断这张图中有多少个哑铃,哑铃判断的条件是,对于一个连通图:如果找到一条边连接这两个点的个 ...

  7. POJ 1498[二分匹配——最小顶点覆盖]

    题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=1498] 题意:给出一个大小为n*n(0<n<100)的矩阵,矩阵中放入m种颜色(标号为1 ...

  8. jquery.rotate.js实现旋转动画

    1.页面引入jquery.rotate.js文件, 下载地址:https://files.cnblogs.com/files/zhoujl-5071/jquery.rotate.js(打开这个网址,c ...

  9. [BOI2004]Sequence

    题面描述 给定整数数组$a_1,a_2,a_3...a_n$,求递增数组$b_1,b_2,b_3...b_n$ 使得$|a_1 - b_1| + |a_2 - b_2| + ... + |a_n - ...

  10. CodeForces - 1009E Intercity Travelling

    题面在这里! 可以发现全是求和,直接拆开算贡献就好了 #include<bits/stdc++.h> #define ll long long using namespace std; c ...