美文网首页
Python3之百度贴吧小爬虫

Python3之百度贴吧小爬虫

作者: 大邓和他的python | 来源:发表于2016-09-10 11:22 被阅读596次

华为贴吧爬虫

import urllib.request
from bs4 import BeautifulSoup
import csv
import time
import random

#计算运行时间
start_time = time.time()

#保存到csv中
csvFile = open(r"E:\Python\Projects\贴吧\华为\huawei.csv",'a+',newline='')
writer =  csv.writer(csvFile)writer.writerow(('posting_num','posting_title','posting_coments_num','posting_user_link','posting_user_name'))

#每页加50,共6940页
base_url = 'http://tieba.baidu.com/f?kw=%E5%8D%8E%E4%B8%BA&ie=utf-8&pn='
posting_num = 1  #计数爬取到第几个帖子
for page in range(0,6942):  #一共6942页   
    time_delay = random.randint(1, 3)  # 设置随机延迟时间,防止频繁的爬取导致百度封锁ID    
    url = base_url + str(page * 50)    
    html = urllib.request.urlopen(url)    
    bsObj = BeautifulSoup(html,'lxml')    
    posting_list = bsObj.find_all('div',{'class':'t_con cleafix'})    #查找标题块内各个信息, 标题、回复数、发帖人       
   
    print('============================')        
    print('正在抓取华为贴吧第%d页' % page)    now_time = time.time()      
    has_spent_seconds = now_time - start_time    
    has_spent_time_int = int((now_time - start_time) / 60)    
    print('华为号小爬虫已耗时%d分钟' % has_spent_time_int)    
    if page > 1:        
        will_need_time = ((6940 * has_spent_seconds) / page)/60        
        will_need_time = int(will_need_time)        
        print('华为号小爬虫还要爬%d分钟'%will_need_time)    
    #页面查找posting_coments_num,
    for posting in posting_list:
        try:
            # posting_coments_num
            posting_coments_num = posting.contents[1].span.contents[0]

            #posting_user_name
            posting_user_name =  posting.contents[3].span.contents[1].a.contents[0]

            #posting_user_link
            posting_user_link = 'http://tieba.baidu.com' + posting.contents[3].span.contents[1].a.attrs['href']

            #posting_title
            posting_title = posting.contents[3].contents[1].contents[1].a.attrs['title']

            #帖子数加1
            posting_num = posting_num + 1

            #数据保存
            writer.writerow((posting_num, posting_title, posting_coments_num, posting_user_link, posting_user_name))

        except:
            continue

    #抓数据每翻一页休息时间
    time.sleep(time_delay)
    #抓取了十页就休息3秒
    if page in list(range(1,6940,10)):
        time.sleep(3)


# 遍历完网站关闭csvFile
csvFile.close()

end_time = time.time()
duration_time = int((end_time - start_time)/60)
print('程序运行了%d分钟'%duration_time)

程序爬了6000页就被百度封掉,你们回去可以改下贴吧的,比如爬小米吧或者其他娱乐的吧,将页面数改成小于6000的,应该不会被封掉。按照我写的代码,我爬6000页用了180分钟。结果如图,403forbiden,被百度封掉了。

QQ截图20160910073601.png

爬这些也可以了,csv文件39M,共计300000条帖子信息。
代码文件链接: https://pan.baidu.com/s/1i4H4MTB 密码: uwb7

每周有直播哦,扫码即可加入

相关文章

  • Python3之百度贴吧小爬虫

    华为贴吧爬虫 程序爬了6000页就被百度封掉,你们回去可以改下贴吧的,比如爬小米吧或者其他娱乐的吧,将页面数改成小...

  • 贴吧帖子内图片抓取

    Python之爬虫练习 利用Python对百度贴吧进行网络爬虫,实现抓取每个帖子内的所有图片并将之保存到本地。 本...

  • 爬取百度贴吧帖子

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

  • python爬虫之百度贴吧

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

  • 百度贴吧高级爬虫

    最近做了一个完整版的百度贴啊全吧爬虫,过程比较具有代表性,分析过来供大家参考。代码在解禁后后贴吧爬虫查看。 项目结...

  • 百度贴吧爬虫

    利用requests、正则表达式和Beautiful爬取贴吧用户名、评论和图片 定义工具类——去除爬取项多余内容 ...

  • 30分钟!用python的request模块抓取百度贴吧内容

    001 我想通过自动抓取百度贴吧 “python爬虫吧“ 里的帖子数据,找到那些付费写爬虫的帖子,赚点零花钱!为了...

  • 多线程爬虫-BeautifulSoup

    线程基类 爬虫百度贴吧中某帖子图片 先要安装BeautifulSoup 参考 BeautifulSoup官网api

  • Python_Scrapy-基础实践

    爬虫实践 获取百度贴吧的内容 以滁州学院吧为例 贴吧地址: 链接的末尾处:&ie=utf-8 表示该连接采用的是u...

  • Python爬虫实战

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

网友评论

      本文标题:Python3之百度贴吧小爬虫

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