python爬虫:从百度贴吧中爬数据
来源:互联网 发布:网络诈骗报警可以追回 编辑:程序博客网 时间:2024/06/05 18:02
python爬虫:从百度贴吧中爬数据
使用的python包
1 from bs4 import Beautifuls
一个十分好用的网页提取的工具包
2 import requests
用于发送各种html请求
3 import re
用编写各种正则语句
分析网页结构
我们所抓取的是百度贴吧的帖子,https://tieba.baidu.com/p/5227503742?see_lz=1&pn=1为书荒吧的一个帖子,其前面部分http://tieba.baidu.com/p/3138733512为网页地址,数字为帖子的标识符。问号?后的为参数,经斜体文本过测试可以猜测出see_lz为是否只看楼主,pn为页数
打开网页,F12后可以看到网页的结构,具体的是要找到我们想要部分的结构,如下:
使用requests获得页面
使用requests发送get请求,得到网页:
def get_page(self,page_num): url = self.base_url+'?see_lz=%s&pn=%s'%(self.see_lz,page_num) try: page = requests.get(url).text return page except requests.RequestException as e: if hasattr(e,'reason'): print('无法连接:%s'%e) return None
得到网页中想要的内容
通过分析网页结构,定位到想要的块,使用beautifulsoup的find,findAll方法匹配快的名称,class,id等信息,具体的使用可以查看beautifulsoup文档
def get_info(self): page = self.get_page(1) soup = BeautifulSoup(page,'lxml') self.page_num = soup.find('li',class_='l_reply_num').findAll('span')[1].string self.page_num = int(self.page_num) self.title = soup.find('h3',class_=re.compile('core_title_txt.*?')).string
定义一个过滤网页不需要信息的类
通过各种正则语句来定位信息,然后对定位的数据进行处理。使用re.compile()
生成正则表达式,使用re.sub()
函数对正则表达式匹配到的字符串进行替换。更多的正则使用方法见正则表达式语法
class Filter: ##去除图片 remove_img = re.compile('<br/><img.*?>') ##去除超链接 remove_link = re.compile('<a.*?>|</a>') ##将制表符td换成\n replace_td = re.compile('<td>') ##将换行符br换成\n replace_br = re.compile('<br/>') ##将其他标签替换为空 remove_tag = re.compile('<.*?>') def filter(self,x): x = re.sub(self.remove_img,'',x) x = re.sub(self.remove_link,'',x) x = re.sub(self.replace_td,'\n',x) x = re.sub(self.replace_br,'\n',x) x = re.sub(self.remove_tag,'',x) return x.strip()
最终的python代码如下
#coding=utf-8from bs4 import BeautifulSoupimport requestsimport reclass Filter: ##去除图片 remove_img = re.compile('<br/><img.*?>') ##去除超链接 remove_link = re.compile('<a.*?>|</a>') ##将制表符td换成\n replace_td = re.compile('<td>') ##将换行符br换成\n replace_br = re.compile('<br/>') ##将其他标签替换为空 remove_tag = re.compile('<.*?>') def filter(self,x): x = re.sub(self.remove_img,'',x) x = re.sub(self.remove_link,'',x) x = re.sub(self.replace_td,'\n',x) x = re.sub(self.replace_br,'\n',x) x = re.sub(self.remove_tag,'',x) return x.strip() class Spider: def __init__(self,base_url,see_lz,txt_path): self.base_url = base_url self.see_lz = see_lz self.page_num = 0 self.title = '' self.m_filter = Filter() self.txt_path = txt_path self.floor = 0 def get_page(self,page_num): url = self.base_url+'?see_lz=%s&pn=%s'%(self.see_lz,page_num) try: page = requests.get(url).text return page except requests.RequestException as e: if hasattr(e,'reason'): print('无法连接:%s'%e) return None def get_info(self): page = self.get_page(1) soup = BeautifulSoup(page,'lxml') self.page_num = soup.find('li',class_='l_reply_num').findAll('span')[1].string self.page_num = int(self.page_num) self.title = soup.find('h3',class_=re.compile('core_title_txt.*?')).string def get_content(self): self.get_info() print('开始写文件(总共%s页):'%self.page_num) file = open(self.txt_path,'w',encoding='utf-8') file.write('----------------<< %s >>-------------\n\n\n'%(self.title)) for i in range(self.page_num): print('第%s页内容写入。。。'%(i+1)) page = self.get_page(i+1) soup = BeautifulSoup(page,'lxml') contents = soup.find('div',class_='p_postlist').contents for content in contents: stance= content.find('div',id=re.compile('post_content_.*?')) if not(stance): continue self.floor += 1 seg = '------------------------------------------ 第%s楼 ----------------------------------\n'%(self.floor) file.write(seg) file.write(self.m_filter.filter(str(stance))+'\n\n') print('写入完成!')if __name__=='__main__': base_url = 'https://tieba.baidu.com/p/5219956465' see_lz = 1 txt_path = 'C:/Users/wgy/Desktop/bdtb.txt' m_spider = Spider(base_url,see_lz,txt_path) m_spider.get_content()
得到的txt文档效果
阅读全文
0 0
- python爬虫:从百度贴吧中爬数据
- python爬虫百度贴吧标题数据
- Python爬虫 百度地图搜索数据采集
- Python爬虫百度照片
- python百度语法爬虫
- python爬虫百度翻译
- Python 百度爬虫图片
- python 东方财富网&百度股票数据定向爬虫 实例
- python爬虫案例——百度贴吧数据采集
- 用Python进行简单的爬虫(从Python百度百科中提取词条)
- python百度贴吧爬虫
- python 爬虫百度搜索结果
- Python简易百度百科爬虫
- 百度图片爬虫-python语言
- python 百度贴吧爬虫
- python爬虫实例--百度风云榜
- python爬虫-百度百科词条
- python- 百度贴吧爬虫
- android sdk 汉化
- Mysql更新,删除与索引(五)
- 使用myeclipse开发的第一个程序
- activate-power-mode 插件 安装 设置 IDEA
- 安卓工作室 美化 修改 汉化
- python爬虫:从百度贴吧中爬数据
- nodejs 中的require
- 欢迎使用CSDN-markdown编辑器
- Coconuts HDU
- ThinkPHP 4种URL 访问模式简介
- android studio 代码模板
- Unity+Behavior Tree行为树 Behavior Designer 行为树组件(三)
- 24.root, alias
- python 获取list tuple dict中最大和最小元素的操作