爬取电影吧一个帖子里的所有楼主发言:

# python2
# -*- coding: utf-8 -*- import urllib2
import string
import re class Baidu_Spider:
feature_pattern = re.compile(r'id="post_content.*?>\s+(.*?)</div>', re.S)
replaceList = [(''', '\''), ('&quot;', '\"')] def __init__(self, url):
self.url = url + '?see_lz=1' def crawl_tieba_lz(self):
begin_page = urllib2.urlopen(self.url).read()
self.print_page_title(begin_page)
count = self.get_page_count(begin_page)
self.handle_data(count) def handle_data(self, count):
f = open('tieba_lz.txt', 'w+')
for i in range(count):
url = self.url + '&pn=' + str(i+1)
hint = '第' + str(i+1) + '页' print '正在下载%s: %s' % (hint, url)
page = urllib2.urlopen(url).read()
features = re.findall(self.feature_pattern, page)
print hint + '下载完成'
print '共有%d条记录' % len(features) f.write(hint + ':\n')
for feature in features:
feature = self.handle_record(feature)
print feature
f.write(feature + '\n\n')
f.close()
print 'done' def handle_record(self, record):
record = re.sub(r'(<|</)br>', '\n', record)
record = re.sub(r'<.*?>', '', record)
for item in self.replaceList:
record = record.replace(item[0], item[1])
return record def get_page_count(self, page):
result = re.search(r'class="red">(\d+?)</span>', page, re.S)
if result:
count = int(result.group(1))
print '一共%d页' % count
else:
count = 0;
print '无法获取页数'
return count def print_page_title(self, page):
result = re.search(r'<h1.*?>(.*?)</h1>', page, re.S)
if result:
title = result.group(1)
print '标题: %s' % title
else:
print '无法获取标题' spider = Baidu_Spider('http://tieba.baidu.com/p/4082863285')
spider.crawl_tieba_lz()

Python爬虫(二)爬百度贴吧楼主发言的更多相关文章

  1. Python爬虫(一)爬百度贴吧

    简单的GET请求: # python2 import urllib2 response = urllib2.urlopen('http://www.baidu.com') html = respons ...

  2. Python 爬虫实例(爬百度百科词条)

    爬虫是一个自动提取网页的程序,它为搜索引擎从万维网上下载网页,是搜索引擎的重要组成.爬虫从一个或若干初始网页的URL开始,获得初始网页上的URL,在抓取网页的过程中,不断从当前页面上抽取新的URL放入 ...

  3. [Python爬虫] Selenium爬取新浪微博客户端用户信息、热点话题及评论 (上)

    转载自:http://blog.csdn.net/eastmount/article/details/51231852 一. 文章介绍 源码下载地址:http://download.csdn.net/ ...

  4. from appium import webdriver 使用python爬虫,批量爬取抖音app视频(requests+Fiddler+appium)

    使用python爬虫,批量爬取抖音app视频(requests+Fiddler+appium) - 北平吴彦祖 - 博客园 https://www.cnblogs.com/stevenshushu/p ...

  5. Python 爬虫实战2 百度贴吧帖子

    爬取百度贴吧的帖子.与上一篇不同的是,这次我们需要用到文件的相关操作. 本篇目标 对百度贴吧的任意帖子进行抓取 指定是否只抓取楼主发帖内容 将抓取到的内容分析并保存到文件 1.URL格式的确定 首先, ...

  6. 初次尝试python爬虫,爬取小说网站的小说。

    本次是小阿鹏,第一次通过python爬虫去爬一个小说网站的小说. 下面直接上菜. 1.首先我需要导入相应的包,这里我采用了第三方模块的架包,requests.requests是python实现的简单易 ...

  7. Python爬虫之爬取慕课网课程评分

    BS是什么? BeautifulSoup是一个基于标签的文本解析工具.可以根据标签提取想要的内容,很适合处理html和xml这类语言文本.如果你希望了解更多关于BS的介绍和用法,请看Beautiful ...

  8. 【Python必学】Python爬虫反爬策略你肯定不会吧?

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 正文 Python爬虫反爬策略三部曲,拥有这三步曲就可以在爬虫界立足了: ...

  9. Python爬虫之爬取站内所有图片

    title date tags layut Python爬虫之爬取站内所有图片 2018-10-07 Python post 目标是 http://www.5442.com/meinv/ 如需在非li ...

随机推荐

  1. Windows下Python3.6.2+Django-1.11.5+httpd-2.4.27-win64-VC14部署网站

    最近项目组正在上python+django.现在将部署过程总结下,相关文件也备份下,面得每次都要弄半天.网上很多不靠谱的做法,让我反复试错,浪费不少时间. 原材料: Python3.6.2 httpd ...

  2. Linux c++ vim环境搭建系列(3)——Ubuntu18.04.4编译安装youcompleteme

    3. youcompleteme编译安装 参考网址: https://github.com/ycm-core/YouCompleteMe#linux-64-bit 建议不要用这个博客的方法: http ...

  3. S7通信协议之你不知道的事儿

    在电气学习的路上,西门子PLC应该是我的启蒙PLC,从早期的S7-300/400 PLC搭建Profibus-DP网络开始接触,到后来的S7-200Smart PLC,再到现在的S7-1200/150 ...

  4. React AntDesign 引入css

    React项目是用umi脚手架搭建的AntDesign,用到一个第三方表格组件Jexcel,npm install 之后组件的样式加载不上,犯了愁,翻阅各种资料,踏平两个小坑. 大家都知道,安装完成的 ...

  5. Java数组 —— 八大排序

    (请观看本人博文--<详解 普通数组 -- Arrays类 与 浅克隆>) 在本人<数据结构与算法>专栏的讲解中,本人讲解了如何去实现数组的八大排序. 但是,在讲解的过程中,我 ...

  6. testNG 下载安装

    testNG的安装方法: 一.在线安装 打开eclipse 选择-->help-->Install New Software选项 ,弹出下面的弹框,Work with框输入 http:// ...

  7. Redis 5.0.9 安装

    目录 系统环境 系统版本 内核版本 安装步骤 安装 gcc 依赖 下载 Redis 解压 Redis 切换到 redis 解压目录下,执行编译 指定目录安装 启动 Redis 服务 最后 系统环境 系 ...

  8. 基于udp协议的套接字通信

    服务端: import socket server=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) server.bind(('127.0.0.1',8 ...

  9. 教你如何快速让浏览器兼容es6

    写在正文前,本来这一节的内容应该放在第二节更合适,因为当时就有同学问ES6的兼容性如何,如何在浏览器兼容ES6的特性,这节会介绍一个抱砖引玉的操作案例. 为什么ES6会有兼容性问题? 由于广大用户使用 ...

  10. 网络找的 关于 “中吹” Janus Dongye

    看了这篇文章,感觉错过了一个精彩的人生. Janus Dongye, Coding Peasant at Universityof Cambridge (2012-present)(剑桥码农,2012 ...