使用BeautifulSoup的简单小爬虫
来源:互联网 发布:淘宝仓库宝贝删除 编辑:程序博客网 时间:2024/06/05 07:54
最近稍微看了点python的入门, runoob上面的入门过了一遍 python的菜鸟教程。网上看爬虫用BeautifulSoup就能简单的尝试下,就学着写了个百度贴吧的,算是小爬虫吧。。。
安装BeautifulSoup
先从官网上down下来 然后解压再用python安装
官网地址 https://www.crummy.com/software/BeautifulSoup/#Download
具体还是网上搜吧 超级多
爬取模块
其实贴吧的网址还是比较容易拼接的 所以有挺多人拿贴吧练手来着
def start(self): for i in range(self.topic_limit/50): self.spide_listpage(i * 50)
因为计划着要翻页嘛 拼接的页码就是这么个格式 做个循环调用方法
def spide_listpage(self, num): url = self.baseUrl + "&pn=" + str(num) html = urllib2.urlopen(url).read() soup = BeautifulSoup(html, 'html.parser') topic_list = soup.findAll('a', attrs={'class': 'j_th_tit '}) for topic in topic_list: if self.keyword in topic['title']: print topic['title'], (self.domain + topic['href']).strip() self.theUrl = (self.domain + topic['href']).strip() break
html就是拼接出来的地址,然后利用beautifulsoup来进行读取,在找到所有class里面带 j_th_tit样式的 然后再把对应的标题和超链接打印出来
这个思路嘛 就是找html里面对应的css样式,毕竟同类的格式肯定是一样的 这个估计大家都懂就不赘述了
然后循环把含有keyword的提取打印出来
文件写入模块
爬取出来索性就写入txt文档好啦
class writeInFile: def __init__(self, url): self.url = url def getTheWeb(self): html = urllib2.urlopen(self.url).read() soup = BeautifulSoup(html, 'html.parser') context_list = soup.findAll('div', 'd_post_content j_d_post_content ') for context in context_list: # print context.text self.wirteFile(context.text) def wirteFile(self, text): with open( 'spider.txt', 'a') as f: f.write(text) f.write('\n')
把刚刚找到的url传入这个方法, 然后调用Beautifulsoup吧帖子里面的文字信息找出来,最后调用python自带的write方法写入到txt里面去
基本还是重复了上一个模块的操作吧
呃。。。这个帖子貌似有点重口味。。下次换个keyword再说吧。
阅读全文
0 0
- 使用BeautifulSoup的简单小爬虫
- 使用beautifulsoup写的第一个小爬虫程序
- 简单爬虫python实现02——BeautifulSoup的使用
- 使用python语言结合beautifulsoup编写简单的网络爬虫
- 使用BeautifulSoup实现简单豆瓣爬虫
- python爬虫--BeautifulSoup的简单用法
- Python---BeautifulSoup 简单的爬虫实例
- 使用BeautifulSoup实现的图片爬虫
- Python爬虫(2)--BeautifulSoup的使用
- Python爬虫小实践:使用BeautifulSoup+Request爬取CSDN博客的个人基本信息
- 一个简单的不用cookie的人人网状态爬取的python爬虫,使用beautifulsoup
- BeautifulSoup模块的简单使用
- python爬虫:BeautifulSoup 使用select方法的使用
- Python使用BeautifulSoup进行爬虫
- python网络爬虫-使用BeautifulSoup
- 爬虫的基本概念及BeautifulSoup的基本使用
- python爬虫由浅入深3--BeautifulSoup的使用的基本方法
- python简单爬虫 及 beautifulSoup简单用法
- ROS学习总结
- eclipse默认指向WebContent目录修改为webRoot 设置说明
- Java
- SSM(十七) MQ应用
- 数据库(mysql)视图作用
- 使用BeautifulSoup的简单小爬虫
- 第6章 传输层
- 第十一周项目3
- 面向接口编程和面向对象编程的区别
- spring boot 中 定时器
- (翻译)Dirichlet-based Histogram Feature Transform for Image Classification
- Flex 布局教程:实例篇
- Unity3D机器学习
- 深度学习里面的一些专业名词