urllib的操作与使用--1

#################################################
‘‘‘
版本:python2.7
编辑器:pycharm
标准库:urllib
header网页头部信息:
server:centos、microsoft-IIs
content-type:text/html;charset=GBK
last-modified:更新信息

‘‘‘
import urllib
#查看urllib中所拥有的方法及变量
# print dir(urllib)
# #利用help来查看urllib帮助文档
print help(urllib)
print help(urllib.urlopen)
url ="http://www.iplaypython.com/"
html = urllib.urlopen(url)
print html.read()
print html.info()

‘‘‘
Date: Fri, 01 Sep 2017 16:16:50 GMT      #美国地区现在时间
Server: Apache                           #linux系统下的apache服务器
Last-Modified: Mon, 28 Aug 2017 02:05:22 GMT    #最进更新时间
ETag: "52316-112e9-557c6ba29dc80"   #etag是表示搜索引擎优化,搜索引擎也是根据这个标签与上面一个标签来判断你的搜索引擎是否更新
Accept-Ranges: bytes
Content-Length: 70377
Vary: Accept-Encoding
Connection: close
Content-Type: text/html
‘‘‘

#获取网页的状态码
print html.getcode()
#玩蛇王是utf-8的编码,所以就不用进行转编码
#测试一个不是utf-8的网站
url1 = "http://www.163.com/"
html1 = urllib.urlopen(url1)
# print html1.read().decode(‘gbk‘).encode(‘utf-8‘)
#查看163.com的头文件信息
# print html1.info()

‘‘‘
结果如下:
Expires: Fri, 01 Sep 2017 16:12:59 GMT
Date: Fri, 01 Sep 2017 16:11:39 GMT              #服务器信息所在地区的现在的时间
Server: nginx                                    #服务器类型(linux或者是windows等)
Content-Type: text/html; charset=GBK             #网页编码
Vary: Accept-Encoding,User-Agent,Accept
Cache-Control: max-age=80
X-Via: 1.1 shq150:6 (Cdn Cache Server V2.0), 1.1 dunyidong72:1 (Cdn Cache Server V2.0)
Connection: close
‘‘‘

#通过获取该网页的状态码来判断该网页是否可以访问,如果输出结果为200则可以访问,出现其他的则不可以访问。
print html1.getcode()
#获取网址
print html1.geturl()

‘‘‘
普及网页的状态码的内容:
200:表示可以正常访问
301:重定向,自己去百度。
404:网页不存在#可以随便创造一个网址来进行判断
403:禁止访问:权限问题/或者是禁止爬虫抓取
500:服务器忙碌
<http权威指南,专门介绍http协议>
web开发,这本书是必备的
‘‘‘

#网页抓取完成后,下载网页
urllib.urlretrieve(url,‘E:\\abc.txt‘)#也可保存一个html文件。
html.close()
html1.close()

########################################################################################
#1 decode()方法之ignore
# import urllib
# url = "http://www.163.com/"
# html = urllib.urlopen(url)
# content = html.read().decode("gbk",‘ignore‘).encode("utf-8")
# print content

# 2 条件判断语句,自动化处理抓取的结果
import urllib
url = "http://www.iplaypython.com/"

html=urllib.urlopen(url)
print html.read()
#相当于:html = urllib.urlopen(url).read()
#Pprint html

# print html.getcode()
#相当于:html = urllib.urlopen(url).getcode()
#print html

# print html.geturl()
# print html.info()

code = html.getcode()
if code ==200:
    print "网页正常"
    print html.read()
    print html.info()
else:
    print "网页有问题"

###############################################################
import urllib
url = "http://www.iplaypython.com"
info = urllib.urlopen(url).info()
print info
‘‘‘
执行结果为:
Date: Sat, 02 Sep 2017 05:08:27 GMT
Server: Apache
Last-Modified: Mon, 28 Aug 2017 02:05:22 GMT
ETag: "52316-112e9-557c6ba29dc80"
Accept-Ranges: bytes
Content-Length: 70377
Vary: Accept-Encoding
Connection: close
Content-Type: text/html
‘‘‘

print info.getparam("charset")
#返回结果为:None   从中可知,有些网站时没有声明头部信息。

#我们修改一个网址来执行
url1="http://www.163.com"
info1=urllib.urlopen(url1).info()
print info1
‘‘‘
在这里为什么要输入一个字符串的参数呢?
其返回值是在哪里获取的(头文件的内容类型中获取的)
Expires: Sat, 02 Sep 2017 05:09:47 GMT
Date: Sat, 02 Sep 2017 05:08:27 GMT
Server: nginx
Content-Type: text/html; charset=GBK
Vary: Accept-Encoding,User-Agent,Accept
Cache-Control: max-age=80
X-Via: 1.1 shq153:8 (Cdn Cache Server V2.0), 1.1 dunyidong75:4 (Cdn Cache Server V2.0)
Connection: close
我们从头文件信息中可知:其内容类型中有charset=GBK,
‘‘‘
print info1.getparam("charset")
#返回结果为:GBK

时间: 2024-08-11 03:40:57

urllib的操作与使用--1的相关文章

urllib and urllib2 and httplib

http://www.cnblogs.com/sysu-blackbear/p/3629420.html http://www.cnblogs.com/wly923/archive/2013/05/07/3057122.html 标记: 2015-06-25  笔记内容整理和更新到51cto 之前使用python中,访问页面网站时,都是使用curl, 再被supprocess的方式,真的很笨. req = "curl --max-time 5 --connect-timeout 5 -o /de

Python爬虫入门这一篇就够了

何谓爬虫 所谓爬虫,就是按照一定的规则,自动的从网络中抓取信息的程序或者脚本.万维网就像一个巨大的蜘蛛网,我们的爬虫就是上面的一个蜘蛛,不断的去抓取我们需要的信息. 爬虫三要素 抓取 分析 存储 基础的抓取操作 1.urllib在Python2.x中我们可以通过urllib 或者urllib2 进行网页抓取,但是再Python3.x 移除了urllib2.只能通过urllib进行操作 import urllib.request response = urllib.request.urlopen(

Python——深入理解urllib、urllib2及requests(requests不建议使用?)

深入理解urllib.urllib2及requests            python Python 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年,Python 源代码同样遵循 GPL(GNU General Public License)协议[1] .Python语法简洁而清晰,具有丰富和强大的类库. urllib and urllib2 区别 urllib和urllib2模块都做与请求URL相关的操作,但

学习Python的urllib模块

 urllib 模块作为Python 3 处理 URL 的组件集合,如果你有 Python 2 的知识,那么你就会注意到 Python 2 中有 urllib 和 urllib2 两个版本的模块,这些现在都是 Python 3 的 urllib 包的一部分,具体如何来体现它们之间的关系 Python 3 的 urllib 模块是一堆可以处理 URL 的组件集合.如果你有 Python 2 的知识,那么你就会注意到 Python 2 中有 urllib 和 urllib2 两个版本的模块.这些现在

urllib包

前言:urllib.parse模块按功能分为两大类:URL parsing(url解析) 和URL quoting(url引用). 一.URL parsing:主要是1.把URL字符串分割成组件2.把组件合并成url字符串 1.1.  urllib.parse.urlparse(urlstring, scheme='', allow_fragments=True) =>操作urlstring字符串生成6个元素组成的一个元祖的ParseResult实体,例 如:ParseResult(scheme

urllib模块中的方法

urllib模块中的方法 1.urllib.urlopen(url[,data[,proxies]]) 打开一个url的方法,返回一个文件对象,然后可以进行类似文件对象的操作.本例试着打开google >>> import urllib >>> f = urllib.urlopen('http://www.google.com.hk/') >>> firstLine = f.readline()   #读取html页面的第一行 >>>

Python 学习之urllib模块---用于发送网络请求,获取数据

1.urllib urllib是Python标准库的一部分,包含urllib.request,urllib.error,urllib.parse,urlli.robotparser四个子模块. (1)urllib.request用法 1)urlopen函数:用于打开一个URL(urlopen返回一个类文件对象,可以像文件一样操作) 例如: import urllib.request web=urllib.request.urlopen('http://www.baidu.com') conten

Python urllib模块urlopen()与urlretrieve()详解

1.urlopen()方法urllib.urlopen(url[, data[, proxies]]) :创建一个表示远程url的类文件对象,然后像本地文件一样操作这个类文件对象来获取远程数据.参数url表示远程数据的路径,一般是网址:参数data表示以post方式提交到url的数据(玩过web的人应该知道提交数据的两种方式:post与get.如果你不清楚,也不必太在意,一般情况下很少用到这个参数):参数proxies用于设置代理.urlopen返回 一个类文件对象,它提供了如下方法:read(

httplib,urllib和urllib2

一.httplib实现了HTTP和HTTPS的客户端协议,一般不直接使用,在python更高层的封装模块中(urllib,urllib2)使用了它的http实现. import httplib conn = httplib.HTTPConnection("google.com") conn.request('get', '/') print conn.getresponse().read() conn.close() httplib.HTTPConnection ( host [ ,