Urllib--爬虫
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--爬虫的更多相关文章
- urllib爬虫(流程+案例)
网络爬虫是一种按照一定规则自动抓取万维网信息的程序.在如今网络发展,信息爆炸的时代,信息的处理变得尤为重要.而这之前就需要获取到数据.有关爬虫的概念可以到网上查看详细的说明,今天在这里介绍一下使用ur ...
- urllib爬虫模块
网络爬虫也称为网络蜘蛛.网络机器人,抓取网络的数据.其实就是用Python程序模仿人点击浏览器并访问网站,而且模仿的越逼真越好.一般爬取数据的目的主要是用来做数据分析,或者公司项目做数据测试,公司业务 ...
- 【Python】python3中urllib爬虫开发
以下是三种方法 ①First Method 最简单的方法 ②添加data,http header 使用Request对象 ③CookieJar import urllib.request from h ...
- python爬虫 urllib模块url编码处理
案例:爬取使用搜狗根据指定词条搜索到的页面数据(例如爬取词条为‘周杰伦'的页面数据) import urllib.request # 1.指定url url = 'https://www.sogou. ...
- [Python]新手写爬虫全过程(已完成)
今天早上起来,第一件事情就是理一理今天该做的事情,瞬间get到任务,写一个只用python字符串内建函数的爬虫,定义为v1.0,开发中的版本号定义为v0.x.数据存放?这个是一个练手的玩具,就写在tx ...
- [Python]爬虫v0.1
#coding:utf-8 import urllib ###### #爬虫v0.1 利用urlib2 和 字符串内建函数 ###### # 获取网页内容 def getHtml(url): page ...
- [Python]新手写爬虫全过程(转)
今天早上起来,第一件事情就是理一理今天该做的事情,瞬间get到任务,写一个只用python字符串内建函数的爬虫,定义为v1.0,开发中的版本号定义为v0.x.数据存放?这个是一个练手的玩具,就写在tx ...
- Python 网络爬虫
爬虫介绍 爬取图片 爬取文本 爬虫相关模块:re 爬虫相关模块:urllib 爬虫相关模块:urllib2 爬虫相关模块:cookielib 爬虫相关模块:requests 爬取需要登录的页面
- vue+node+mongoDB火车票H5(七)-- nodejs 爬12306查票接口
菜鸟一枚,业余一直想做个火车票查票的H5,前端页面什么的已经写好了,node+mongoDB 也写了一个车站的接口,但 接下来的爬12306获取车次信息数据一直卡住,网上的爬12306的大部分是pyt ...
- 爬虫---request+++urllib
网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本.另外一些不常使用的名字还有蚂蚁.自动索引.模拟程序或者蠕 ...
随机推荐
- SpringBoot-02:SpringBoot中的POM文件详细解释
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 我把pom文件,以及它的详细解释发出来 <?xml version="1.0" en ...
- C# 组装XML传给webserver+XML 返回获取多个xml,根据多个XML 返回dataset类型
大致流程介绍: 传值给 webserver+XML ,得到webserver+XML多个返回值,组装成dataset形式返回 首先创建所需要的类型 DataSet ds = new DataSet() ...
- hdu1231最大连续子序列(动态规划)
最大连续子序列 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- VMware 15.0下载及安装教程
虚拟机 VMware WorkStation Pro15 下载及安装详细解 9虚拟机 VMware WorkStation Pro15 下载及安装详细解. 虚拟机官方网站: https://www.v ...
- IDEA搭载Tomcat使用JSTL连接Oracle数据库
1.在IDEA中,JSTL库添加到WEB-INF/lib下面可以直接在JSP页面上通过 <%@ taglib uri="http://java.sun.com/jsp/jstl/cor ...
- 【halcon】学习记录
图像采集和二值化等处理 * Image Acquisition : Code generated by Image Acquisition open_framegrabber (, , , , , , ...
- 深度学习笔记 (二) 在TensorFlow上训练一个多层卷积神经网络
上一篇笔记主要介绍了卷积神经网络相关的基础知识.在本篇笔记中,将参考TensorFlow官方文档使用mnist数据集,在TensorFlow上训练一个多层卷积神经网络. 下载并导入mnist数据集 首 ...
- JS判断是IOS还是Android以及如何解决h5打包后在ios下内容与状态栏重叠问题
h5打包后在ios下内容与状态栏重叠问题: 1:知道设备的类型: var u = navigator.userAgent, app = navigator.appVersion; var isAndr ...
- HTML5form表单的相关知识总结
首先在介绍HTML5form表单的新增内容之前,我总结了一下HTML的form表单的内容. <!DOCTYPE html> <html lang="en"> ...
- Thunder团队第一周 - Scrum会议5
本节内容: 工作照片 会议时间 会议地点 会议内容 Todo list 燃尽图 Scrum会议5 小组名称:Thunder 项目名称:爱阅app Scrum Master:邹双黛 工作照片: 参会成员 ...