python2与python3爬虫中get与post对比解析
2019-10-11 18:07
1481 查看
python2中的urllib2改为python3中的urllib.request
四种方式对比:
python2的get
# coding=utf-8 import urllib import urllib2 word = urllib.urlencode({"wd":"百度"}) url = 'http://www.baidu.com/s' + '?' + word request = urllib2.Request(url) print urllib2.urlopen(request).read().decode('utf-8')
python3的get
import urllib.request import urllib.parse data = urllib.parse.urlencode({'wd':'百度'}) url = 'http://wwww.baidu.com/s?' + data # url = 'http://www.baidu.com/s?wd=' + urllib.parse.quote('百度') response = urllib.request.urlopen(url) print (response.read().decode('utf-8'))
python2的post
# coding=utf-8 import urllib import urllib2 formdata = { 'name':'百度' } data = urllib.urlencode(formdata) request = urllib2.Request(url = "http://httpbin.org/post", data=data) response = urllib2.urlopen(request) print response.read()
python3的post
import urllib.parse import urllib.request data = bytes(urllib.parse.urlencode({'name':'百度'}),encoding='utf8') response = urllib.request.urlopen('http://httpbin.org/post',data=data) print(response.read().decode('utf-8'))
或
import urllib.parse import urllib.request request = urllib.request.Request('http://httpbin.org/post',data=bytes(urllib.parse.urlencode({'name':'百度'}),encoding='utf8))')) response = urllib.request.urlopen(request) print (response.read().decode('utf-8'))
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
相关文章推荐
- 03—小白学Python爬虫之urllib的基本和进阶使用及Get、Post示例
- python 爬虫 《一》get请求 post请求 +伪装
- python爬虫中get和post方法介绍以及cookie作用
- python爬虫笔记<一:POST和GET数据传送>
- Python爬虫实战(三种数据解析方式及对比总结)
- Python爬虫中的Get和Post方法
- Python爬虫-04:贴吧爬虫以及GET和POST的区别
- python爬虫 urllib模块发起post请求过程解析
- Python爬虫2-GET_POST与开发者工具
- Python爬虫中的Get和Post方法
- python的get和post方法解析
- python爬虫get和post方法的使用以及cookie
- Python3 爬虫--网页get和post
- python爬虫 基于requests模块发起ajax的get请求实现解析
- Python爬虫(二)——urllib库,Post与Get数据传送区别,设置Headers,urlopen方法,简单爬虫
- 03Python爬虫---延时以及GET和POST请求
- python爬虫(五)_urllib2:Get请求和Post请求
- Python的Bottle框架中实现最基本的get和post的方法的教程
- 爬虫GET&POST提交方法
- 通过网络图片小爬虫对比Python中单线程与多线(进)程的效率