在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/Python/ parse.urlencode和request.Request

parse.urlencode和request.Request

from urllib import request,parse

url = 'http://so.123.com.cn/'
headers = {
    'User_Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64)AppleWebKit/537.36                 (KHTML, like Gecko) Chrome/53.0.2785.21 Safari/537.36',
    'Referer':'http://so.123.com.cn/'
}
dict = {
    'name':'hello'
}
data = bytes(parse.urlencode(dict),encoding='utf-8') #這串代碼啥意思???打印了一下data,結(jié)果是name=hello,這個(gè)有啥作用呢?
req=urllib.request.Request(url=url,data=data,headers=headers,method='POST')#method傳POST就說(shuō)明是POST請(qǐng)求?這句代碼是不是相當(dāng)于把幾個(gè)參數(shù)打包起來(lái),賦給req后面一起處理???
response = request.urlopen(req)
print(response.read().decode('utf-8'))

回答
編輯回答
真難過(guò)

parse.urlencode方法的作用是把dict格式的參數(shù)轉(zhuǎn)換為url參數(shù),并以u(píng)tf-8編碼,可以拼接為HTTP請(qǐng)求。


In [1]: from urllib.parse import urlencode
   ...: urlencode({'a':1,'b':'你好'})
   ...:
Out[1]: 'a=1&b=%E4%BD%A0%E5%A5%BD'
2017年1月10日 16:23
編輯回答
尤禮

注意不要覆蓋內(nèi)置方法 dict

dict = {
    'name': 'hello'
}
2018年7月9日 04:58