python requests get/post

基本Get請求:
   
   
   
   
#-*- coding:utf-8 -*-import requestsurl = 'http://www.baidu.com'r = requests.get(url)print r.text
修改header的get請求:
   
   
   
   
import requestsheaders = {"Authorization": "Bearer 4SMf3bbEWuzD8tGxM7Kg9LQr4RZY7xpEPgbHde5AKGFd63CHvNajtDN3PoACybLLqce1dwa9kld2ketBUpqwvZZG41SqPXw7Mtnr","User-Agent": "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2","Host": "api.connector.mbed.com","Accept": "*/*"}s = requests.get("https://api.connector.mbed.com/endpoints/",headers = headers)print s.request.headersprint s.headersprint s.text
帶參數Get請求:
    
    
    
    
#-*- coding:utf-8 -*-import requestsurl = 'http://www.baidu.com'payload = {'key1': 'value1', 'key2': 'value2'}r = requests.get(url, params=payload)print r.text
POST請求模擬登錄及一些返回對象的方法:
    
    
    
    
#-*- coding:utf-8 -*-import requestsurl1 = 'http://www.exanple.com/login'#登錄地址url2 = "http://www.example.com/main"#須要登錄才能訪問的地址data={"user":"user","password":"pass"}headers = { "Accept":"text/html,application/xhtml+xml,application/xml;", "Accept-Encoding":"gzip", "Accept-Language":"zh-CN,zh;q=0.8", "Referer":"http://www.example.com/", "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36" }res1 = requests.post(url1, data=data, headers=headers)res2 = requests.get(url2, cookies=res1.cookies, headers=headers)print res2.content#得到二進制響應內容print res2.raw#得到原始響應內容,須要stream=Trueprint res2.raw.read(50)print type(res2.text)#返回解碼成unicode的內容print res2.urlprint res2.history#追蹤重定向print res2.cookiesprint res2.cookies['example_cookie_name']print res2.headersprint res2.headers['Content-Type']print res2.headers.get('content-type')print res2.json#講返回內容編碼爲jsonprint res2.encoding#返回內容編碼print res2.status_code#返回http狀態碼print res2.raise_for_status()#返回錯誤狀態碼
使用Session()對象的寫法(Prepared Requests):
    
    
    
    
#-*- coding:utf-8 -*-import requestss = requests.Session()url1 = 'http://www.exanple.com/login'#登錄地址url2 = "http://www.example.com/main"#須要登錄才能訪問的地址data={"user":"user","password":"pass"}headers = { "Accept":"text/html,application/xhtml+xml,application/xml;", "Accept-Encoding":"gzip", "Accept-Language":"zh-CN,zh;q=0.8", "Referer":"http://www.example.com/", "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36" }prepped1 = requests.Request('POST', url1, data=data, headers=headers).prepare()s.send(prepped1)'''也能夠這樣寫res = requests.Request('POST', url1,data=data,headers=headers)prepared = s.prepare_request(res)# do something with prepped.body# do something with prepped.headerss.send(prepared)'''prepare2 = requests.Request('POST', url2, headers=headers).prepare()res2 = s.send(prepare2)print res2.content
另外一種寫法 :
    
    
    
    
#-*- coding:utf-8 -*-import requestss = requests.Session()url1 = 'http://www.exanple.com/login'#登錄地址url2 = "http://www.example.com/main"#須要登錄才能訪問的頁面地址data={"user":"user","password":"pass"}headers = { "Accept":"text/html,application/xhtml+xml,application/xml;", "Accept-Encoding":"gzip", "Accept-Language":"zh-CN,zh;q=0.8", "Referer":"http://www.example.com/", "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36" }res1 = s.post(url1, data=data)res2 = s.post(url2)print(resp2.content)
其餘的一些請求方式:
    
    
    
    
>>> r = requests.put("http://httpbin.org/put")>>> r = requests.delete("http://httpbin.org/delete")>>> r = requests.head("http://httpbin.org/get")>>> r = requests.options("http://httpbin.org/get")


遇到的問題:
在cmd下執行,遇到個小錯誤:
    
    
    
    
UnicodeEncodeError:'gbk' codec can't encode character u'\xbb' in position 23460: illegal multibyte sequence
分析:
一、Unicode是編碼仍是解碼
    
    
    
    
UnicodeEncodeError
很明顯是在編碼的時候出現了錯誤
 
二、用了什麼編碼
    
    
    
    
'gbk' codec can't encode character
使用GBK編碼出錯
 
解決辦法:
肯定當前字符串,好比
    
    
    
    
#-*- coding:utf-8 -*-import requestsurl = 'http://www.baidu.com'r = requests.get(url)print r.encoding>utf-8
已經肯定html的字符串是utf-8的,則能夠直接去經過utf-8去編碼。
    
    
    
    
print r.text.encode('utf-8')
參考連接: 官方文檔

問題二:
TypeError: 'unicode' object is not callable報錯
   
   
   
   
Traceback (most recent call last): File "F:\git\mbed_webapp\webapp.py", line 12, in <module> print s.text()TypeError: 'unicode' object is not callable
解決方法:
在Python中,出現'unicode' object is not callable的錯誤通常是把字符串當作函數使用了。
參考:http://www.cnblogs.com/xiongjiaji/p/3615943.html


相關文章
相關標籤/搜索