@songying
2017-11-23T08:18:06.000000Z
字数 1300
阅读 1088
python库
# 该方法会对网络进行更加深入的控制
req = urllib.request.Request(url) //发送请求 #(url,data,header)
response = urllib.request.urlopen(req)
response = urllib.request.urlopen(url)
response.geturl() # 获取URL地址
response.url
response.getcode() # 获取状态码
response.code
response.info() # HTTP响应报文的头部信息
# 1 获得response流 -- 字节流必须使用这种方式
html = response.read()
# 2 把response 流 编码为 utf8
html = response.read().decode('utf-8')
request.type # 请求的类型,https
request.unredirected_hdrs # request的头
request.host # 获取host地址
request.full_url #获取完整url
req.add_header() # 添加头部信息
from urllib import request
response = request.urlopen('网址') //获取HTTP响应报文对象
headers = response.info()
headers['date'] //访问响应头部中的date
for line in response:
print(line.decode('utf-8').rstrip()) //获取读取到的网页内容
query_args = {'q': 'query string', 'foo': 'bar'}
encoded_args = parse.urlencode(query_args)
url = '网址' + encoded_args
response = request.urlopen(url)
from urllib import parse
from urllib import request
query_args = {'q': 'query string', 'foo': 'bar'}
encoded_args = parse.urlencode(query_args).encode('utf-8')
url = 'https://pymotw.com/3/urllib.request/index.html'
print(request.urlopen(url, data=encoded_args).read().decode('utf-8'))
r = request.Request('http://localhost:8080/')
r.add_header(
'User-agent',
'PyMOTW (https://pymotw.com/)',
)
response = urllib.request.urlopen(r)