您现在的位置是:首页 > 文章详情

python模块: urllib模块详解 !!

日期:2019-02-18点击:407

前言
相信大家在做一个Python项目的时候,肯定会用到模块,就想JAVA会用到JAR包一样,这样会让我们的开发更高效和更简洁,本节为大家介绍一个URLlib模块

在python中urllib模块提供上层接口,可以使用它下载读取数据,这里举个例子,把sina首页的html抓取下来显示出来.有2种方法可以实现.

1.urlopen(url, data=None, proxies=None) urlopen(url [, data]) -> open file-like object

创建一个表示远程url的类文件对象,然后像本地文件一样操作这个类文件对象来获取远程数据。参数url表示远程数据的路径,一般是网址;参数data表示以post方式提交到url的数据;参数proxies用于设置代理.urlopen返回一个类文件对象.

 #!/usr/bin/python2.5 import urllib url = "http://www.sina.com" data = urllib.urlopen(url).read() print data``` 

root@10.1.6.200:~# python gethtml.py



<meta http-equiv="Content-type" content="text/html; charset=gb2312" /> <title>тK˗ҳ</title> <meta name="keywords" content="тK,тKθ,SINA,sina,sina.com.cn,тK˗ҳ,ą»§,؊Ѷ" />

....`

2 urlretrieve(url, filename=None, reporthook=None, data=None)

urlretrieve方法直接将远程数据下载到本地。参数filename指定了保存到本地的路径(如果未指定该参数,urllib会生成一个临时文件来保存数据);参数reporthook是一个回调函数,当连接上服务器.以及相应的数据块传输完毕的时候会触发该回调.

 #!/usr/bin/python2.5 import urllib url = "http://www.sina.com" path = "/root/sina.txt" data = urllib.urlretrieve(url,path)``` 

root@10.1.6.200:~# python getsina.py
root@10.1.6.200:~# cat sina.txt



<meta http-equiv="Content-type" content="text/html; charset=gb2312" /> <title>тK˗ҳ</title> <meta name="keywords" content="тK,тKθ,SINA,sina,sina.com.cn,тK˗ҳ,ą»§,؊Ѷ" />

....

不仅如此,这里写个爬虫小程序,可以把百度贴吧http://tieba.baidu.com/p/2236567282网页上的jpg图片依次下载下来. 

root@10.1.6.200:~# cat getJpg.py

!/usr/bin/python2.5

import re
import urllib

def getHtml(url):

html = urllib.urlopen(url).read() return html 

def getJpg(html):

reg = r'src="(http://.*?\.jpg)"' imgre = re.compile(reg) imgList = re.findall(imgre,html) x = 0 for imgurl in imgList: urllib.urlretrieve(imgurl,'%s.jpg' % x) x += 1 

html = getHtml("http://tieba.baidu.com/p/2236567282")
getJpg(html)`

 root@10.1.6.200:~# python 11.py root@10.1.6.200:~# ls -l total 1680 -rw-r--r-- 1 root root 38695 2013-04-11 23:32 0.jpg -rw-r--r-- 1 root root 48829 2013-04-11 23:32 10.jpg -rw-r--r-- 1 root root 51835 2013-04-11 23:32 11.jpg -rw-r--r-- 1 root root 41688 2013-04-11 23:32 12.jpg -rw-r--r-- 1 root root 1077 2013-04-11 23:32 13.jpg -rw-r--r-- 1 root root 33989 2013-04-11 23:32 14.jpg -rw-r--r-- 1 root root 41890 2013-04-11 23:32 15.jpg -rw-r--r-- 1 root root 35728 2013-04-11 23:32 16.jpg -rw-r--r-- 1 root root 44405 2013-04-11 23:32 17.jpg -rw-r--r-- 1 root root 29847 2013-04-11 23:32 18.jpg -rw-r--r-- 1 root root 44607 2013-04-11 23:32 19.jpg -rw-r--r-- 1 root root 23939 2013-04-11 23:32 1.jpg -rw-r--r-- 1 root root 45592 2013-04-11 23:32 20.jpg -rw-r--r-- 1 root root 60910 2013-04-11 23:32 2.jpg -rw-r--r-- 1 root root 39014 2013-04-11 23:32 3.jpg -rw-r--r-- 1 root root 19057 2013-04-11 23:32 4.jpg -rw-r--r-- 1 root root 64584 2013-04-11 23:32 5.jpg -rw-r--r-- 1 root root 29297 2013-04-11 23:32 6.jpg -rw-r--r-- 1 root root 39145 2013-04-11 23:32 7.jpg -rw-r--r-- 1 root root 1059 2013-04-11 23:32 8.jpg
原文链接:https://yq.aliyun.com/articles/690753
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章