贴吧的老历史想存下来,强行python爬取一波

队友太强,躺好别动,偶尔做点副业

假装会python

基本流程:

1.爬取页面,获取页面的html源码

2.分析源码 通过正则表达式 匹配到想要的内容

3.去掉不需要的

python中 re模块 提供对正则表达式的支持

 # -*- coding:utf-8 -*-
import urllib2
import urllib
import re
class Tool:
#去除img标签,7位长空格
removeImg = re.compile('<img.*?>| {7}|')
#删除超链接标签
removeAddr = re.compile('<a.*?>|</a>')
#把换行的标签换为\n
replaceLine = re.compile('<tr>|<div>|</div>|</p>')
#将表格制表<td>替换为\t
replaceTD= re.compile('<td>')
#把段落开头换为\n加空两格
replacePara = re.compile('<p.*?>')
#将换行符或双换行符替换为\n
replaceBR = re.compile('<br><br>|<br>')
#将其余标签剔除
removeExtraTag = re.compile('<.*?>')
def replace(self,x):
x = re.sub(self.removeImg,"",x)
x = re.sub(self.removeAddr,"",x)
x = re.sub(self.replaceLine,"\n",x)
x = re.sub(self.replaceTD,"\t",x)
x = re.sub(self.replacePara,"\n ",x)
x = re.sub(self.replaceBR,"\n",x)
x = re.sub(self.removeExtraTag,"",x)
#strip()将前后多余内容删除
return x.strip()
class BDTB: def __init__(self,baseUrl,seeLZ):
self.baseURL =baseUrl
self.seeLZ ='?see_lz'+str(seeLZ)
self.tool=Tool()
def getPage(self,pageNum):#抓取网页 第几页
try:
url=self.baseURL+self.seeLZ+'&pn='+str(pageNum)
request = urllib2.Request(url)
response =urllib2.urlopen(request)
#print response.read()
return response.read().decode('utf-8')
except urllib2.URLError,e:
if hasattr(e,"reason"):
print u"fail",e.reason
return None
def getTitle(self):#获取标题
page =self.getPage(1)
pattern = re.compile('<h3 class="core_title_txt.*?>(.*?)</h3>',re.S)
result =re.search(pattern,page)
#print result
if result:
print result.group(1)
return result.group(1).strip()
else :
return None
def getContent(self):#获取正文
page=self.getPage(1)
pattern =re.compile('<div id="post_content_.*?>(.*?)</div>',re.S)
items =re.findall(pattern,page)
for i in range(1,20):#前19个
print self.tool.replace(items[i]) baseURL = 'http://tieba.baidu.com/p/5285925491'#任意一个网页的url
bdtb=BDTB(baseURL,1)
#bdtb.getTitle()
bdtb.getContent()

假装会python--爬取贴吧正文的更多相关文章

  1. Python爬取视频指南

    摘自:https://www.jianshu.com/p/9ca86becd86d 前言 前两天尔羽说让我爬一下菜鸟窝的教程视频,这次就跟大家来说说Python爬取视频的经验 正文 https://w ...

  2. 用Python爬取网易云音乐热评

    用Python爬取网易云音乐热评 本文旨在记录Python爬虫实例:网易云热评下载 由于是从零开始,本文内容借鉴于各种网络资源,如有侵权请告知作者. 要看懂本文,需要具备一点点网络相关知识.不过没有关 ...

  3. Python 爬取所有51VOA网站的Learn a words文本及mp3音频

    Python 爬取所有51VOA网站的Learn a words文本及mp3音频 #!/usr/bin/env python # -*- coding: utf-8 -*- #Python 爬取所有5 ...

  4. python爬取网站数据

    开学前接了一个任务,内容是从网上爬取特定属性的数据.正好之前学了python,练练手. 编码问题 因为涉及到中文,所以必然地涉及到了编码的问题,这一次借这个机会算是彻底搞清楚了. 问题要从文字的编码讲 ...

  5. python爬取某个网页的图片-如百度贴吧

    python爬取某个网页的图片-如百度贴吧 作者:vpoet mail:vpoet_sir@163.com 注:随意copy,不用告诉我 #coding:utf-8 import urllib imp ...

  6. Python:爬取乌云厂商列表,使用BeautifulSoup解析

    在SSS论坛看到有人写的Python爬取乌云厂商,想练一下手,就照着重新写了一遍 原帖:http://bbs.sssie.com/thread-965-1-1.html #coding:utf- im ...

  7. 使用python爬取MedSci上的期刊信息

    使用python爬取medsci上的期刊信息,通过设定条件,然后获取相应的期刊的的影响因子排名,期刊名称,英文全称和影响因子.主要过程如下: 首先,通过分析网站http://www.medsci.cn ...

  8. python爬取免费优质IP归属地查询接口

    python爬取免费优质IP归属地查询接口 具体不表,我今天要做的工作就是: 需要将数据库中大量ip查询出起归属地 刚开始感觉好简单啊,毕竟只需要从百度找个免费接口然后来个python脚本跑一晚上就o ...

  9. Python爬取豆瓣指定书籍的短评

    Python爬取豆瓣指定书籍的短评 #!/usr/bin/python # coding=utf-8 import re import sys import time import random im ...

  10. python爬取网页的通用代码框架

    python爬取网页的通用代码框架: def getHTMLText(url):#参数code缺省值为‘utf-8’(编码方式) try: r=requests.get(url,timeout=30) ...

随机推荐

  1. MFC Edit控件的使用~~

    EditBox,一般用于显示数字文本,或者与用户沟通获取数字文本. 这里介绍一种将EditBox与一个变量关联起来的方法: 快捷键:Shift + Ctrl + X,进入类导向,选择成员变量属性页: ...

  2. lm393

    电压比较芯片,供电电压和输出电压一致.

  3. 设计模式 笔记 解释器模式 Interpreter

    //---------------------------15/04/26---------------------------- //Interpreter 解释器模式----类行为型模式 /* 1 ...

  4. stl源码剖析 详细学习笔记 hashset hashmap

    //---------------------------15/03/26---------------------------- //hash_set { /* hash_set概述: 1:这是一个 ...

  5. jira webhook 事件触发并程序代码调用jenkins接口触发构建操作

    要解决的问题 开发管理工具触发站点构建事件,事件处理中需要调用Jenkins接口开始构建动作. 我的应用场景: 使用jira作为管理工具,在jira中创建自定义的工作流来规定测试,上线,发布等流程,并 ...

  6. 2014.9.11 Research Meeting Report

    Dear All: Yesterday when we read INFOCOM papers, you have seen how damage it is to have careless wri ...

  7. metasploit学习之情报搜集

    3.1.被动信息搜集whois查询Netcraft nslookup>set type=mx>testfire.net Google Hacking 3.2 主动信息搜集 使用nmap进行 ...

  8. kafka学习总结之集群部署和zookeeper

    1.  集群部署 kafka集群的瓶颈主要在网络和磁盘上:kafka依赖于zookeeper,zookeeper集群的节点采用奇数个,3个节点允许一个节点失败,5个节点允许2个节点失败. 图 1 ka ...

  9. alpha版发布

    网站网址:http://doeverying.applinzi.com/

  10. scrapy 爬虫怎么写入日志和保存信息

    写入日志: 首先我的爬虫 name= article scrapy crawl article -s LOG_FILE=wiki.log 输出为不同格式: scrapy crawl article - ...