码迷,mamicode.com
首页 > 编程语言 > 详细

python2与python3爬虫get与post对比

时间:2019-09-18 00:53:54      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:baidu   decode   encoding   utf-8   code   odi   orm   python2   方式   

  1. python2中的urllib2改为python3中的urllib.request

  2. 四种方式对比:

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'))

python2与python3爬虫get与post对比

标签:baidu   decode   encoding   utf-8   code   odi   orm   python2   方式   

原文地址:https://www.cnblogs.com/kolane/p/11538071.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!