这篇文章讲述的是使用urllib库来获取网络页面资源,如有错误或不当之处,还望各大神批评指正。
一、基本用法
- 使用urllib.request.urlopen()方法发送请求
# 基本用法
response = urllib.request.urlopen(request)
二、发送请求
- 例子:获取百度首页
import urllib.request
def get_page():
with urllib.request.urlopen('http://www.baidu.com') as response:
print(response.read().decode('utf-8'))
pass
get_page()
注:使用with…as…语句调用,这样会更有利于在不使用时正 常关闭连接。返回的结果是HTTPResponse对象。调用这个对象的read()方法,可以访问具体的文件内容。
三、读取网页内容
- 使用r.read()方法读取网页内容
- 解决中文乱码问题
例子读取百度网页内容
import urllib.request
def get_page():
with urllib.request.urlopen('http://www.baidu.com') as r:
print(r.read().decode('utf-8'))
get_page()
四、理解httpResponse方法
HTTPResponse对象是一种类文件对象,除了可以文件的read()方法读取它的内容外,还有别的属性和方法。 例如:r.code与r.status属性存放本次请求的响应码;r.headers属性存放响应头;r.url属性存放了发出响应的服务 器URL;还可以尝试info()和geturl()方法。使用response的geturl()和info方法来验证请求与响应是否如我们希 望的一样。有时会出现请求发往的服务器与应答服务器不是同一台主机的情况。
- 获取状态码
- 获取响应url
- 获取响应头
- 获取响应信息
- 获取响应状态
- 读取网页内容
例子:打印百度响应信息
import urllib.request
def get_page():
with urllib.request.urlopen('http://www.baidu.com') as r:
print(r.code)
print(r.status)
print(r.headers)
print(r.url)
print(r.info())
print(r.geturl())
get_page()
运行结果
200
200
Bdpagetype: 1
Bdqid: 0xc376d3460002c709
Cache-Control: private
Content-Type: text/html
Cxy_all: baidu+08eddd09e5ca831d850913a468f3d393
Date: Fri, 03 May 2019 05:33:34 GMT
Expires: Fri, 03 May 2019 05:33:24 GMT
P3p: CP=" OTI DSP COR IVA OUR IND COM "
Server: BWS/1.1
Set-Cookie: BAIDUID=69FBD35425C2F8E81369526810A1C266:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BIDUPSID=69FBD35425C2F8E81369526810A1C266; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: PSTM=1556861614; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: delPer=0; path=/; domain=.baidu.com
Set-Cookie: BDSVRTM=0; path=/
Set-Cookie: BD_HOME=0; path=/
Set-Cookie: H_PS_PSSID=28883_1466_28937_21109_28775_28721_28964_28832_28584_26350; path=/; domain=.baidu.com
Vary: Accept-Encoding
X-Ua-Compatible: IE=Edge,chrome=1
Connection: close
Transfer-Encoding: chunked
http://www.baidu.com
Bdpagetype: 1
Bdqid: 0xc376d3460002c709
Cache-Control: private
Content-Type: text/html
Cxy_all: baidu+08eddd09e5ca831d850913a468f3d393
Date: Fri, 03 May 2019 05:33:34 GMT
Expires: Fri, 03 May 2019 05:33:24 GMT
P3p: CP=" OTI DSP COR IVA OUR IND COM "
Server: BWS/1.1
Set-Cookie: BAIDUID=69FBD35425C2F8E81369526810A1C266:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BIDUPSID=69FBD35425C2F8E81369526810A1C266; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: PSTM=1556861614; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: delPer=0; path=/; domain=.baidu.com
Set-Cookie: BDSVRTM=0; path=/
Set-Cookie: BD_HOME=0; path=/
Set-Cookie: H_PS_PSSID=28883_1466_28937_21109_28775_28721_28964_28832_28584_26350; path=/; domain=.baidu.com
Vary: Accept-Encoding
X-Ua-Compatible: IE=Edge,chrome=1
Connection: close
Transfer-Encoding: chunked
http://www.baidu.com