美文网首页
python爬取百度贴吧

python爬取百度贴吧

作者: 有苦向瓜诉说 | 来源:发表于2017-03-24 22:34 被阅读0次

1.对百度贴吧的任意帖子进行抓取

2.指定是否只抓取楼主发帖内容

3.将抓取到的内容分析并保存到文件

import re
import bs4
from bs4 import BeautifulSoup
import requests

class TiebaSpider(object):

    def __init__(self,see_lz):
        self.see_lz=see_lz

    def getHTMLText(self,url,pageNumber):
        try:
            headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0'}
            r=requests.get(url+str(pageNumber),timeout=30,headers=headers)
            r.raise_for_status()
            r.encoding='utf-8'
            return r.text
        except:
            return 'ERROR'

    def getTitle(self,html):
        try:
            title=re.search(r'<h3 class="core_title_txt pull-left text-overflow.*?>(.*?)</h3>',html)
            return title.group(1).strip()
        except:
            return '百度贴吧'

    def getContent(self,html):
        floors=[]
        soup=BeautifulSoup(html,'html.parser')#,attrs={'class':'d_post_content j_d_post_content'}
        contents=soup.find_all('div',attrs={'class':'d_post_content j_d_post_content '})
        for content in contents:
            item=content.get_text()
            floors.append(item)
        return floors

    def getPageNumber(self,html):
        soup=BeautifulSoup(html,'html.parser')
        li=soup.find('li',attrs={'class':'l_reply_num'})
        pageNumber=li.find_all('span')[1].string
        #print(pageNumber)
        return int(pageNumber)

    def writeFile(self,title,contents):
        if title is None or contents is None:
            return 'ERROR'
        floor=0
        with open(title+'.txt','a',encoding='utf-8') as f: 
            for item in contents:
                floor=floor+1
                floorline=str(floor)+u'楼------------------------------------------------------------------------------'+'\n'
                f.write(floorline+item+'\n'+'\n')

    def start(self):
        start_url='https://tieba.baidu.com/p/3138733512?'
        url=start_url+'see_lz='+str(self.see_lz)+'&pn='
        html=self.getHTMLText(url,1)
        title=self.getTitle(html)
        pageNumber=self.getPageNumber(html)
        contents=self.getContent(html)
        count=0
        self.writeFile(title,contents)
        for i in range(2,pageNumber+1):
            count=count+1
            print('\r当前进度{:.2f}%'.format(count*100/pageNumber),end='')
            html=self.getHTMLText(url,i)
            contents=self.getContent(html)
            self.writeFile(title,contents)

baidu=TiebaSpider(1)
baidu.start()

相关文章

  • python爬取百度贴吧

    爬取百度贴吧python文件源代码如下(欢迎点赞哦) import urllib.request import u...

  • python爬取百度贴吧

    爬取百度贴吧python文件源代码如下(欢迎点赞哦) import urllib.request import u...

  • Python爬虫实战

    注:采转归档,自己学习查询使用 Python爬虫实战(1):爬取糗事百科段子Python爬虫实战(2):百度贴吧帖...

  • python爬取百度贴吧的图片1

    python版本:2.7.10学习python爬虫,首先写了一个爬取百度贴吧图片的程序。参考了静觅的系列博客 好了...

  • python爬取百度贴吧

    1.对百度贴吧的任意帖子进行抓取 2.指定是否只抓取楼主发帖内容 3.将抓取到的内容分析并保存到文件

  • python爬虫之百度贴吧

    最近又尝试着爬取了百度贴吧,发现新增的几个反爬点,故来做下记录。 爬取百度贴吧大致流程为: 1 - 构造url,h...

  • 爬取百度贴吧帖子

    依然是参考教程 Python爬虫实战一之爬取百度贴吧帖子。作者崔庆才写了很多关于Python爬虫的文章,大家有兴趣...

  • Go基础编程---web编程

    简单web服务器代码 简单客户端代码 并发爬取百度贴吧的页面 并发爬虫爬取段子

  • 1.爬虫入门_爬取html网页

    1.开发环境python2.7 2.爬取贴吧页面代码实现

  • python爬取百度贴吧的图片2

    今天看了一下beautifulsoup库的用法,把昨天的python爬取百度贴吧的图片1的代码更新成使用beaut...

网友评论

      本文标题:python爬取百度贴吧

      本文链接:https://www.haomeiwen.com/subject/hhjqottx.html