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优步司机奖励政策(3月29日)

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

  2. Mac iTem2 自动登录服务器配置

    假设你要连接的服务器地址为123.123.123.123,端口号为8888,用户名为root,密码为mimamima 编写shell文件"login_server.sh",并放置于 ...

  3. LeetCode: 59. Spiral Matrix II(Medium)

    1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...

  4. restTemplate访问接口

    后端技术精选 每天推送精选技术好文,涉及Java.python.Linux及MySQL,欢迎关注微信公众号:后端技术精选 随笔 - 52, 文章 - 0, 评论 - 50, 引用 - 0 Spring ...

  5. uvaoj1225Digit Counting(暴力)

    Trung is bored with his mathematics homeworks. He takes a piece of chalk and starts writing a sequen ...

  6. C++ 基础面试题-2

    请写出一下程序的输出内容 /* ** 2018/03/21 22:02:03 ** Brief: ** Author:ZhangJianWei ** Email:Dream_Dog@163.com * ...

  7. 如何往eclipse中导入maven项目

    现在公司中大部分项目可能都是使用maven来构建,假如现在摆在你面前有一个maven的项目,如果你要学习它,如何将它导入到像eclipse这样的集成开发工具中呢,以项目public_class_1为例 ...

  8. Java进阶知识点:并发容器背后的设计理念

    一.背景 容器是Java编程中使用频率很高的组件,但Java默认提供的基本容器(ArrayList,HashMap等)均不是线程安全的.当容器和多线程并发编程相遇时,程序员又该何去何从呢? 通常有两种 ...

  9. LeetCode 109——有序链表转化二叉搜索树

    1. 题目 2. 解答 2.1. 方法一 在 LeetCode 108--将有序数组转化为二叉搜索树 中,我们已经实现了将有序数组转化为二叉搜索树.因此,这里,我们可以先遍历一遍链表,将节点的数据存入 ...

  10. 关闭Tomcat进程 一条语句(必看)

    写在开始 MAC系统下进行JAVA研发,经常遇到的一个问题就是杀死异常Tomcat 通常都是用两条指令,先查询出Tomcat占用的进程,再kill掉该进程, 其实有一种联合语句的方式可以一条语句直接关 ...