python2與python3爬蟲中g(shù)et與post對比解析
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'))
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用 Python 讀取電子表格中的數(shù)據(jù)實例詳解
這篇文章主要介紹了使用 Python 讀取電子表格中的數(shù)據(jù),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
Django Haystack 全文檢索與關(guān)鍵詞高亮的實現(xiàn)
這篇文章主要介紹了Django Haystack 全文檢索與關(guān)鍵詞高亮的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-02-02
pydev debugger: process 10341 is co
這篇文章主要介紹了pydev debugger: process 10341 is connecting無法debu的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
Python3+Appium安裝及Appium模擬微信登錄方法詳解
這篇文章主要介紹了Python3+Appium安裝及使用方法詳解,需要的朋友可以參考下2021-02-02
python爬蟲實戰(zhàn)之最簡單的網(wǎng)頁爬蟲教程
在我們?nèi)粘I暇W(wǎng)瀏覽網(wǎng)頁的時候,經(jīng)常會看到一些好看的圖片,我們就希望把這些圖片保存下載,或者用戶用來做桌面壁紙,或者用來做設(shè)計的素材。下面這篇文章就來給大家介紹了關(guān)于利用python實現(xiàn)最簡單的網(wǎng)頁爬蟲的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-08-08
Python過濾掉numpy.array中非nan數(shù)據(jù)實例
這篇文章主要介紹了Python過濾掉numpy.array中非nan數(shù)據(jù)實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
linux mint中搜狗輸入法導致pycharm卡死的問題
這篇文章主要介紹了linux mint中搜狗輸入法導致pycharm卡死的問題,這篇文章給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10

