urllib.request.Request的用法
来源:互联网 发布:the rain 久石让 知乎 编辑:程序博客网 时间:2024/05/29 12:30
我们可以利用urlopen()方法可以实现最基本请求的发起,但这几个简单的参数并不足以
构建一个完整的请求,如果请求中需要加入headers(请求头)等信息,我们就可以利用
更强大的Request类来构建一个请求。也许这就是一下两个方式的区别吧:
respponse = urllib.request.urlopen(url)
req = urllib.request.Request(url)response = urllib.request.urlopen(req)#用Request类构建了一个完整的请求,增加了headers等一些信息
import urllib.requestimport urllib.parseurl = 'http://httpbin.org/post'headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}headers['Host'] = 'httpbin.org'dict = {'name':'Germey'}data = urllib.parse.urlencode(dict).encode('utf-8')#data参数如果要传必须传bytes(字节流)类型的,如果是一个字典,先用urllib.parse.urlencode()编码。request = urllib.request.Request(url = url,data = data,headers = headers,method = 'POST')response = urllib.request.urlopen(request)html = response.read().decode('utf-8')print(html)
第二种设置urllib.request.Request中data,headers以及method的方法:
req = urllib.request.Request(url=url,data=data,method='POST')req.add_headers('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36')
阅读全文
0 0
- urllib.request.Request的用法
- urllib.request
- Python3: urllib.request 的使用
- Python urllib.request 的使用
- Urllib.request用法简单介绍(Python3.3)
- Python中urllib.request模块的Request对象
- python3的urllib的request模块
- python3 urllib.request抓资源的方式
- 使用python 3的urllib.request库
- Python3中urllib.request.retrieve的使用
- python3 urllib.request
- python urllib.request
- urllib 和 request 对比
- Python3:urllib.request详解
- urllib 和 request 对比
- 【转】Urllib.request用法简单介绍(Python3.3)
- request对象的用法
- request.getSession的用法
- codeforce 812CSagheer and Nubian Market(二分查找)
- 连接池中的maxIdle,MaxActive,maxWait等参数详解
- ES2015数组扩展
- 第三章 Linux特殊字符和正则表达式
- 把视频数据写到文件里面去来分析
- urllib.request.Request的用法
- 封装UGUI的点击事件
- JavaScript类型转换
- spring-boot笔记-Servlet、过滤器、监听器(三)
- Oracle sql语句日常整理
- 学习链接汇总
- 网页跳转到APP
- pat1031-1040
- Java基础教程2-安装和配置Eclipse IDE环境