[关闭]
@songying 2017-11-23T08:18:06.000000Z 字数 1300 阅读 1088

python标准库urllib.request

python库


urllib.request

访问网络的两种方式

  1. # 该方法会对网络进行更加深入的控制
  2. req = urllib.request.Request(url) //发送请求 #(url,data,header)
  3. response = urllib.request.urlopen(req)
  1. response = urllib.request.urlopen(url)

response对象

  1. response.geturl() # 获取URL地址
  2. response.url
  3. response.getcode() # 获取状态码
  4. response.code
  5. response.info() # HTTP响应报文的头部信息
  6. # 1 获得response流 -- 字节流必须使用这种方式
  7. html = response.read()
  8. # 2 把response 流 编码为 utf8
  9. html = response.read().decode('utf-8')

request对象

  1. request.type # 请求的类型,https
  2. request.unredirected_hdrs # request的头
  3. request.host # 获取host地址
  4. request.full_url #获取完整url
  5. req.add_header() # 添加头部信息

HTTP GET

  1. from urllib import request
  2. response = request.urlopen('网址') //获取HTTP响应报文对象
  3. headers = response.info()
  4. headers['date'] //访问响应头部中的date
  5. for line in response:
  6. print(line.decode('utf-8').rstrip()) //获取读取到的网页内容

Http Get(带参数)

  1. query_args = {'q': 'query string', 'foo': 'bar'}
  2. encoded_args = parse.urlencode(query_args)
  3. url = '网址' + encoded_args
  4. response = request.urlopen(url)

HTTP POST(带参数)

  1. from urllib import parse
  2. from urllib import request
  3. query_args = {'q': 'query string', 'foo': 'bar'}
  4. encoded_args = parse.urlencode(query_args).encode('utf-8')
  5. url = 'https://pymotw.com/3/urllib.request/index.html'
  6. print(request.urlopen(url, data=encoded_args).read().decode('utf-8'))

添加请求头部信息

  1. r = request.Request('http://localhost:8080/')
  2. r.add_header(
  3. 'User-agent',
  4. 'PyMOTW (https://pymotw.com/)',
  5. )
  6. response = urllib.request.urlopen(r)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注