什么是Rullib?
Python内置的http请求库
发送请求
-
"""
-
urllib.request模块提供了人最基本的构造http请求的方法,利用它可以模拟浏览器的一个发起过程
-
"""
-
#案例1
-
if __name__ == '__main__':
-
import urllib.request
-
import socket
-
import urllib.error
-
try:
-
response = urllib.request.urlopen("http://www.python.org",timeout=0.1)
-
except urllib.error.URLError as e:
-
if isinstance(e.reason,socket.timeout):
-
print("TIME OUT")
-
print(response.read().decode('utf8'))#返回网页源代码,得到源代码之后,其中的信息就能很好的提起出来了
-
print(type(response))#返回相应类型
-
print(response.status)#返回响应状态码
-
print(response.getheaders())#获取响应头
-
print(response.getheader('Server'))#获取响应头中的一部分内容
-
"""
-
其他的参数:
-
data: 可选参数,如果使用该方法,则请求方式改为POST
-
timeout: 用于设置超时时间,单位为秒,如果不指定,则使用全局默认时间
-
"""