這篇文章主要介紹了response.content和response.text的區(qū)別,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)致力于互聯(lián)網(wǎng)網(wǎng)站建設(shè)與網(wǎng)站營(yíng)銷,提供網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)、網(wǎng)站開(kāi)發(fā)、seo優(yōu)化、網(wǎng)站排名、互聯(lián)網(wǎng)營(yíng)銷、微信小程序定制開(kāi)發(fā)、公眾號(hào)商城、等建站開(kāi)發(fā),創(chuàng)新互聯(lián)網(wǎng)站建設(shè)策劃專家,為不同類型的客戶提供良好的互聯(lián)網(wǎng)應(yīng)用定制解決方案,幫助客戶在新的全球化互聯(lián)網(wǎng)環(huán)境中保持優(yōu)勢(shì)。1. response.content和response.text的區(qū)別
response.content是編碼后的byte類型(“str”數(shù)據(jù)類型),response.text是unicode類型。這兩種方法的使用要視情況而定。注意:unicode -> str 是編碼過(guò)程(encode()); str -> unicode 是解碼過(guò)程(decode())。示例如下:
# --coding:utf-8-- # import requests response = requests.get("https://baidu.com/") print response.url print type(response.content) with open("C:\\Users\\Administrator\\Desktop\\content.html", "w") as f: f.write(response.content) print "content保存成功" print type(response.text) with open("C:\\Users\\Administrator\\Desktop\\text.html", "w") as f: # 返回url的編碼方式 print response.encoding f.write(response.text.encode("ISO-8859-1")) print "text保存成功"
2. 發(fā)送get請(qǐng)求,直接調(diào)用“resquests.get" 就可以了。response的一些屬性:response.text; response.content; response.url; response.encoding; response.status_code
# --coding:utf-8-- # import requests params = { "wd": "中國(guó)" } headers = { "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36" } response = requests.get("https://baidu.com/s", params=params, headers=headers) print response.url with open("C:\\Users\\Administrator\\Desktop\\get.html", "w") as f: f.write(response.content) print "保存成功"
3. 發(fā)送post請(qǐng)求:傳入data信息。注意get請(qǐng)求傳入的是params信息。示例如下:
# --coding:utf-8-- # import requests data = { "first": "true", "pn": "1", "wd": "python" } headers = { "Referer": "https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=", "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36" } response = requests.post("https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false", data=data, headers=headers) print response.encoding print type(response.content) with open("C:\\Users\\Administrator\\Desktop\\post.html", "w") as f: f.write(response.content) print "保存成功"
4. 使用代理。在get方法中增加proxy參數(shù)即可。示例代碼如下:
# --coding:utf-8-- # import requests proxy = { "http": "124.42.7.103" } response = requests.get("http://httpbin.org/ip", proxies=proxy) print response.content
5. requests處理cookies信息。使用requests.Session()方法即可。示例代碼如下:
# --coding:utf-8-- # import requests url = "http://www.renren.com/PLogin.do" # url = "http://www.renren.com/SysHome.do" data = {"email": "賬號(hào)", "password": "密碼"} headers = { "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36" } session = requests.Session() session.post(url, data=data, headers=headers) response = session.get("http://www.renren.com/543484094/profile") with open("C:\\Users\\Administrator\\Desktop\\Liwei.html", "w") as fp: fp.write(response.content) print "保存成功"
6. 處理不信任的SSL證書(shū)。與上面的代碼相比,多了一個(gè)verify=False參數(shù),為了處理SSL證書(shū)不受信用的問(wèn)題。
示例代碼如下:
response = session.get("http://www.renren.com/543484094/profile", verify=False)
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“response.content和response.text的區(qū)別”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián)建站,關(guān)注創(chuàng)新互聯(lián)網(wǎng)站制作公司行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
網(wǎng)頁(yè)標(biāo)題:response.content和response.text的區(qū)別-創(chuàng)新互聯(lián)
鏈接URL:http://jinyejixie.com/article30/djsipo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、微信公眾號(hào)、網(wǎng)站營(yíng)銷、網(wǎng)站制作、App設(shè)計(jì)、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容