爬虫06 代码封装(以爬虫04为基础)
来源:互联网 发布:章淘客cms 编辑:程序博客网 时间:2024/05/20 05:54
爬虫04的代码对于coder来说更易读些,但是对于面向对象而言还需进行封装,仅以此作为示范
# -*- coding: utf-8 -*-import urllibimport urllib2import reclass QSBK: def __init__(self): self.page = 1 self.url = 'http://www.qiushibaike.com/8hr/page/%d/?s=4908781' %self.page self.user_agent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0" self.headers = { 'User-Agent' : self.user_agent } def get_response(self,url,headers): request = urllib2.Request(self.url,headers=self.headers) response = urllib2.urlopen(request) back=response.read() return back def get_joke(self): back=self.get_response(self.url,self.headers) imglist=re.findall(r'<div[^>]class="content">\n\n([^<]+)<[^>]+.+\n\n[^<]',back) return imglist def write_joke(self): imglist=self.get_joke() f = open('糗事百科'+'.txt', 'w') for joke in imglist: print joke f.write(joke) def _main_(self): self.write_joke()spyider=QSBK()spyider._main_()
0 0
- 爬虫06 代码封装(以爬虫04为基础)
- 零基础简单爬虫制作(以wjyt-china企业黄页爬虫为例)(上)
- 零基础简单爬虫制作(以wjyt-china企业黄页爬虫为例)(中)
- 零基础简单爬虫制作(以wjyt-china企业黄页爬虫为例)(下)
- 正则表达式基础——以Python爬虫为实例
- python实现爬虫--以CSDN为例
- 网络爬虫剖析,以Pyspider为例
- 最基础的爬虫代码
- python爬虫-->爬虫基础
- Python 基础爬虫简介(测试环境为 Python 2.7)
- [Python]网络爬虫(十):一个爬虫的诞生全过程(以山东大学绩点运算为例)
- [Python]网络爬虫(十):一个爬虫的诞生全过程(以山东大学绩点运算为例)
- [Python]网络爬虫(十):一个爬虫的诞生全过程(以山东大学绩点运算为例)
- [Python]网络爬虫(十):一个爬虫的诞生全过程(以山东大学绩点运算为例)
- [Python]网络爬虫(十):一个爬虫的诞生全过程(以山东大学绩点运算为例)
- [Python]网络爬虫(十):一个爬虫的诞生全过程(以山东大学绩点运算为例)
- [Python]网络爬虫(十):一个爬虫的诞生全过程(以山东大学绩点运算为例)
- [Python]网络爬虫(十):一个爬虫的诞生全过程(以山东大学绩点运算为例)
- 卡特兰数相关知识
- 对于RTTI机制的理解
- TCP/IP详解,卷1:协议(1)
- Amigo 源码解读
- node安装图解
- 爬虫06 代码封装(以爬虫04为基础)
- 稀缺:我们如何陷入贫穷与忙碌的
- 前端插件日常问题 整理
- exec函数讲解
- 成本中心通过利润中心来和公司代码对应
- 如何用 Git 将代码恢复到一个历史的版本
- 【MySQL 07】数据库恢复技术
- React Native Android ScrollView 去除阴影效果
- 09 ExpanableListView 的代码例子