1.简单爬虫

from urllib import request

def f(url):
print('GET: %s' % url)
resp = request.urlopen(url) #赋给一个实例,请求
data = resp.read() #把结果读出来
f=open('url.html','wb')
f.write(data)
f.close()
print('%d bytes received from %s.' % (len(data), url)) f('http://www.cnblogs.com/alex3714/articles/5248247.html')

运行结果:

C:\abccdxddd\Oldboy\python-3.5.2-embed-amd64\python.exe C:/abccdxddd/Oldboy/Py_Exercise/Day10/爬虫.py
GET: http://www.cnblogs.com/alex3714/articles/5248247.html
91829 bytes received from http://www.cnblogs.com/alex3714/articles/5248247.html. Process finished with exit code 0

2.爬多个网页

from urllib import request
import gevent def f(url):
print('GET: %s' % url)
resp = request.urlopen(url) #赋给一个实例,请求
data = resp.read() #把结果读出来
print('%d bytes received from %s.' % (len(data), url)) #启动3个协程并且传参数
gevent.joinall([
gevent.spawn(f, 'https://www.python.org/'),
gevent.spawn(f, 'https://www.yahoo.com/'),
gevent.spawn(f, 'https://github.com/'),
])

运行结果:

GET: https://www.python.org/
48751 bytes received from https://www.python.org/.
GET: https://www.yahoo.com/
479631 bytes received from https://www.yahoo.com/.
GET: https://github.com/
55394 bytes received from https://github.com/. Process finished with exit code 0

3.测试运行时间:

from urllib import request
import gevent
import time def f(url):
print('GET: %s' % url)
resp = request.urlopen(url) #赋给一个实例,请求
data = resp.read() #把结果读出来
print('%d bytes received from %s.' % (len(data), url)) start_time=time.time()
#启动3个协程并且传参数
gevent.joinall([
gevent.spawn(f, 'https://www.python.org/'),
gevent.spawn(f, 'https://www.yahoo.com/'),
gevent.spawn(f, 'https://github.com/'),
])
print('cost is %s:'%(time.time()-start_time))

运行结果:通过时间看到也是串行运行的。gevent默认检测不到 urllib 进行的是否是io操作。

C:\abccdxddd\Oldboy\python-3.5.2-embed-amd64\python.exe C:/abccdxddd/Oldboy/Py_Exercise/Day10/爬虫.py
GET: https://www.python.org/
48751 bytes received from https://www.python.org/.
GET: https://www.yahoo.com/
488624 bytes received from https://www.yahoo.com/.
GET: https://github.com/
55394 bytes received from https://github.com/.
cost is 4.5304529666900635: Process finished with exit code 0

4.同步与异步的时间比较:

from urllib import request
import gevent
import time
#from gevent import monkey #monkey.patch_all() #把当前程序的所有io操作给我单独地做上标记
def f(url):
print('GET: %s' % url)
resp = request.urlopen(url) #赋给一个实例,请求
data = resp.read() #把结果读出来
print('%d bytes received from %s.' % (len(data), url)) urls=['https://www.python.org/','https://www.yahoo.com/','https://github.com/']
start_time=time.time()
for url in urls:
f(url)
print('同步cost is %s:'%(time.time()-start_time)) async_time_start=time.time() #异步的起始时间
gevent.joinall([
gevent.spawn(f, 'https://www.python.org/'),
gevent.spawn(f, 'https://www.yahoo.com/'),
gevent.spawn(f, 'https://github.com/'),
])
print('异步cost is %s:'%(time.time()-async_time_start))

运行时间:几乎差不多,看不出异步的优势。

C:\abccdxddd\Oldboy\python-3.5.2-embed-amd64\python.exe C:/abccdxddd/Oldboy/Py_Exercise/Day10/爬虫.py
GET: https://www.python.org/
48751 bytes received from https://www.python.org/.
GET: https://www.yahoo.com/
480499 bytes received from https://www.yahoo.com/.
GET: https://github.com/
55394 bytes received from https://github.com/.
同步cost is 7.112711191177368:
GET: https://www.python.org/
48751 bytes received from https://www.python.org/.
GET: https://www.yahoo.com/
485666 bytes received from https://www.yahoo.com/.
GET: https://github.com/
55390 bytes received from https://github.com/.
异步cost is 4.510450839996338: Process finished with exit code 0

5.因为gevent默认检测不到 urllib 进行的是否是io操作。要想让两者关联起来,需要再导入一个新函数(补丁)

from gevent import monkey,
monkey.patch_all()
from urllib import request
import gevent
import time
from gevent import monkey monkey.patch_all() #把当前程序的所有io操作给我单独地做上标记
def f(url):
print('GET: %s' % url)
resp = request.urlopen(url) #赋给一个实例,请求
data = resp.read() #把结果读出来
print('%d bytes received from %s.' % (len(data), url)) urls=['https://www.python.org/','https://www.yahoo.com/','https://github.com/']
start_time=time.time()
for url in urls:
f(url)
print('同步cost is %s:'%(time.time()-start_time)) async_time_start=time.time() #异步的起始时间
gevent.joinall([
gevent.spawn(f, 'https://www.python.org/'),
gevent.spawn(f, 'https://www.yahoo.com/'),
gevent.spawn(f, 'https://github.com/'),
])
print('异步cost is %s:'%(time.time()-async_time_start))

运行结果:

C:\abccdxddd\Oldboy\python-3.5.2-embed-amd64\python.exe C:/abccdxddd/Oldboy/Py_Exercise/Day10/爬虫.py
GET: https://www.python.org/
48751 bytes received from https://www.python.org/.
GET: https://www.yahoo.com/
487577 bytes received from https://www.yahoo.com/.
GET: https://github.com/
55392 bytes received from https://github.com/.
同步cost is 5.784578323364258:
GET: https://www.python.org/
GET: https://www.yahoo.com/
GET: https://github.com/
480662 bytes received from https://www.yahoo.com/.
48751 bytes received from https://www.python.org/.
55394 bytes received from https://github.com/.
异步cost is 1.8721871376037598: Process finished with exit code 0

Urllib--爬虫的更多相关文章

  1. urllib爬虫(流程+案例)

    网络爬虫是一种按照一定规则自动抓取万维网信息的程序.在如今网络发展,信息爆炸的时代,信息的处理变得尤为重要.而这之前就需要获取到数据.有关爬虫的概念可以到网上查看详细的说明,今天在这里介绍一下使用ur ...

  2. urllib爬虫模块

    网络爬虫也称为网络蜘蛛.网络机器人,抓取网络的数据.其实就是用Python程序模仿人点击浏览器并访问网站,而且模仿的越逼真越好.一般爬取数据的目的主要是用来做数据分析,或者公司项目做数据测试,公司业务 ...

  3. 【Python】python3中urllib爬虫开发

    以下是三种方法 ①First Method 最简单的方法 ②添加data,http header 使用Request对象 ③CookieJar import urllib.request from h ...

  4. python爬虫 urllib模块url编码处理

    案例:爬取使用搜狗根据指定词条搜索到的页面数据(例如爬取词条为‘周杰伦'的页面数据) import urllib.request # 1.指定url url = 'https://www.sogou. ...

  5. [Python]新手写爬虫全过程(已完成)

    今天早上起来,第一件事情就是理一理今天该做的事情,瞬间get到任务,写一个只用python字符串内建函数的爬虫,定义为v1.0,开发中的版本号定义为v0.x.数据存放?这个是一个练手的玩具,就写在tx ...

  6. [Python]爬虫v0.1

    #coding:utf-8 import urllib ###### #爬虫v0.1 利用urlib2 和 字符串内建函数 ###### # 获取网页内容 def getHtml(url): page ...

  7. [Python]新手写爬虫全过程(转)

    今天早上起来,第一件事情就是理一理今天该做的事情,瞬间get到任务,写一个只用python字符串内建函数的爬虫,定义为v1.0,开发中的版本号定义为v0.x.数据存放?这个是一个练手的玩具,就写在tx ...

  8. Python 网络爬虫

    爬虫介绍 爬取图片 爬取文本 爬虫相关模块:re 爬虫相关模块:urllib 爬虫相关模块:urllib2 爬虫相关模块:cookielib 爬虫相关模块:requests 爬取需要登录的页面

  9. vue+node+mongoDB火车票H5(七)-- nodejs 爬12306查票接口

    菜鸟一枚,业余一直想做个火车票查票的H5,前端页面什么的已经写好了,node+mongoDB 也写了一个车站的接口,但 接下来的爬12306获取车次信息数据一直卡住,网上的爬12306的大部分是pyt ...

  10. 爬虫---request+++urllib

    网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本.另外一些不常使用的名字还有蚂蚁.自动索引.模拟程序或者蠕 ...

随机推荐

  1. 成都Uber优步司机奖励政策(1月23日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  2. Sql Server 2008R2中使用CET进行递归查询

            在使用数据库的过程中,我们经常会遇到递归的查询.比如传入一个分类ID,要查出这个分类下的所有子分类,以及子分类的子分类.或者说传入一个部门ID,要查出这个部门下所有子部门的人员:在Or ...

  3. 客户端SDK测试思路

    本文来自网易云社区 作者:万春艳 是什么 客户端SDK是为第三方开发者提供的软件开发工具包,包括SDK接口.开发文档和Demo示例等.SDK和应用之间是什么关系呢?以云信即时消息服务为例,如下图所示, ...

  4. android学习八 多用途碎片

    碎片设计初衷:帮助开发人员管理应用程序功能. 特点:1.大量重用           2.可用性强           3.适应多种布局            碎片 1.包含一个视图层次结构和具有相应 ...

  5. 内置方法(item系列)

    class Foo: def __init__(self,name): self.name = name def __getitem__(self, item): # 获取时触发 print('get ...

  6. Java Swing学习笔记——创建JFrame

    创建显示一个空JFrame import javax.swing.JFrame; public class JFrameDemo extends JFrame{ public JFrameDemo() ...

  7. 1.1.0 Unity零基础入门2——Roll a Ball

    1. 游戏界面 2.代码 //FoodRotate - - 控制cube旋转 using System.Collections; using System.Collections.Generic; u ...

  8. Java开发工程师(Web方向) - 03.数据库开发 - 第5章.MyBatis

    第5章--MyBatis MyBatis入门 Abstract: 数据库框架的工作原理和使用方法(以MyBatis为例) 面向对象的世界与关系型数据库的鸿沟: 面向对象世界中的数据是对象: 关系型数据 ...

  9. JS实现对数组的去重

    JS实现对数组的去重 $scope.validateContect = function(text) { var arr = text; // 若传入的数据为string类型,用逗号分隔 if((ty ...

  10. Django学习总结之模板templates

    - django模板: templates - 模板分为两个过程: 1, 加载 : loader.get_template('xxx.html') 返回值是一个template对象 2, 渲染 : t ...