Python爬虫从入门到懵逼-1
来源:互联网 发布:活动致辞知乎 编辑:程序博客网 时间:2024/06/03 22:56
封装自己的简易爬虫框架
1.框架封装
#--coding:utf-8--import urllib2class my_crawler:#我的爬虫类 python类定义:结束 如果要继承则在冒号前用括号 可以多继承 def __init__(self,url,path):#Python中的构造方法 self 相当于Java的this self._url=url self._path=path def read_resource(self):#读取资源的方法 raw_url=urllib2.quote(self._url) #考虑到中文问题这里用quote编码 url=urllib2.unquote(raw_url)#unquote解码 return urllib2.urlopen(url,timeout=5).read()#打开网页 设置超时时间 读取内容 def write_resource(self): try: f=open(self._path,'wb')#open打开文件 wb二进制写入 f.write(self.read_resource()) #写文件 f.close()#关闭文件 print (self._url+"信息成功爬取并写入"+self._path) except Exception as e:#尝试捕获异常 print ("出现异常"+e)
2.框架使用
#--coding:utf-8--from my_crawler_framework import my_crawler#从my_crawler_framework模块中引入my_crawler类url="http://www.baidu.com/s?wd=肥猫下楼吃面包"#定义需爬取的网址path="d:/img/crawlers/zp.html"#文件写入路径framework=my_crawler(url,path)#利用构造方法创建my_crawler对象framework.write_resource()#调用my_crawler对象的write_resource方法
阅读全文
0 0
- Python爬虫从入门到懵逼-1
- Python爬虫从入门到懵逼-0
- python爬虫从入门到精通全套
- Python爬虫从敲门到入门
- Python爬虫从入门到死亡(一)-什么是爬虫
- python爬虫入门1--爬虫基本结构
- Python 爬虫入门 1 了解爬虫Scrapy
- python爬虫入门到懵逼-2
- python 网络爬虫入门 1
- Python爬虫入门(1)
- Python爬虫 | Python爬虫入门
- 【python爬虫】python爬虫入门攻略(1)
- python爬虫入门简单爬虫
- Python爬虫笔记----爬虫技术入门(1)
- Python爬虫入门级(1) - 爬虫程序必备模块
- Python爬虫入门(1):综述
- Python爬虫入门(1):综述
- python 爬虫入门1 网页图片保存
- iOS 网络高级编程二:HTTP并没有那么简单
- ajax跨域jsonp的两种解决办法
- 事先规划RTL结构
- TCP协议用在python和wifi模块之间
- 可能与不可能的边界:P/NP问题趣史
- Python爬虫从入门到懵逼-1
- 如何让子元素居于父元素底部
- hibernate之持久化类,主键生成策略,事务,缓存,查询api(02)
- 2012 浙大机试 Sharing
- 1026. 程序运行时间
- Git使用详细教程
- PyCharm 查看各种常用类型的方法
- The first one.
- [渗透&攻防] 二.SQL MAP工具从零解读数据库及基础用法