Python鍩虹瀛︿範涔媟equests鍩虹(涓�)

PS锛氭枃绔犳憳鑷笅闈㈣繖浣嶅ぇ绁炵殑閾炬帴銆�

浣滆�咃細AlexMercer313

閾炬帴锛歨ttps://www.jianshu.com/p/d662205283a4

渚嗘簮锛氱畝涔�

Requests鏄敤Python璇█缂栧啓锛屽熀浜巙rllib锛岄噰鐢ˋpache2 Licensed寮�婧愬崗璁殑HTTP搴撱�傚畠姣攗rllib鏇村姞鏂逛究锛屽彲浠ヨ妭绾︽垜浠ぇ閲忕殑宸ヤ綔锛屽畬鍏ㄦ弧瓒矵TTP娴嬭瘯闇�姹傘�俁equests鐨勫摬瀛︽槸浠EP 20鐨勪範璇负涓績寮�鍙戠殑锛屾墍浠ュ畠姣攗rllib鏇村姞Pythoner銆傛洿閲嶈鐨勪竴鐐规槸瀹冩敮鎸丳ython3鍝︼紒

Beautiful is better than ugly.(缇庝附浼樹簬涓戦檵)

Explicit is better than implicit.(娓呮浼樹簬鍚硦)

Simple is better than complex.(绠�鍗曚紭浜庡鏉�)

Complex is better than complicated.(澶嶆潅浼樹簬绻佺悙)

Readability counts.(閲嶈鐨勬槸鍙鎬�)

涓�銆佸畨瑁卹equest

閫氳繃pip瀹夎

pip install requests

鎴栬�咃紝涓嬭浇浠g爜鍚庡畨瑁咃細

$ git clone git://github.com/kennethreitz/requests.git$ cd requests

$ python setup.pyinstall

鍐嶆噿涓�鐐癸紝閫氳繃IDE瀹夎鍚э紝濡俻ycharm锛�

浜屻�佸彂閫佽姹備笌浼犻�掑弬鏁�

鍏堟潵涓�涓畝鍗曠殑渚嬪瓙鍚э紒璁╀綘浜嗚В涓嬪叾濞佸姏锛�

importre quests

r= requests.get(url='http://www.itwhy.org')#鏈�鍩烘湰鐨凣ET璇锋眰

print(r.status_code)#鑾峰彇杩斿洖鐘舵��

r = requests.get(url='http://dict.baidu.com/s',params={'wd':'python'})#甯﹀弬鏁扮殑GET璇锋眰

print(r.url)

print(r.text)#鎵撳嵃瑙g爜鍚庣殑杩斿洖鏁版嵁

寰堢畝鍗曞惂锛佷笉浣咷ET鏂规硶绠�鍗曪紝鍏朵粬鏂规硶閮芥槸缁熶竴鐨勬帴鍙f牱寮忓摝锛�

requests.get(鈥榟ttps://github.com/timeline.json鈥�)#GET璇锋眰

requests.post(鈥渉ttp://httpbin.org/post鈥�)#POST璇锋眰

requests.put(鈥渉ttp://httpbin.org/put鈥�)#PUT璇锋眰

requests.delete(鈥渉ttp://httpbin.org/delete鈥�)#DELETE璇锋眰

requests.head(鈥渉ttp://httpbin.org/get鈥�)#HEAD璇锋眰

requests.options(鈥渉ttp://httpbin.org/get鈥�)#OPTIONS璇锋眰

PS锛氫互涓婄殑HTTP鏂规硶锛屽浜嶹EB绯荤粺涓�鑸彧鏀寔 GET 鍜� POST锛屾湁涓�浜涜繕鏀寔 HEAD 鏂规硶銆�

甯﹀弬鏁扮殑璇锋眰瀹炰緥锛�

import requests

requests.get('http://www.dict.baidu.com/s',params={'wd':'python'})#GET鍙傛暟瀹炰緥requests.post('http://www.itwhy.org/wp-comments-post.php',data={'comment':'娴嬭瘯POST'})#POST鍙傛暟瀹炰緥

POST鍙戦�丣SON鏁版嵁锛�

import requests聽

import json

r= requests.post('https://api.github.com/some/endpoint',data=json.dumps({'some':'data'}))print(r.json())

瀹氬埗header锛�

import requests聽

importjson

data= {'some':'data'}

headers= {'content-type':'application/json','User-Agent':'Mozilla/5.0(X11;Ubuntu;Linux x86_64;rv:22.0)Gecko/20100101 Firefox/22.0'}

r= requests.post('https://api.github.com/some/endpoint',data=data,headers=headers)print(r.text)

涓夈�乺esponse瀵硅薄

浣跨敤requests鏂规硶鍚庯紝浼氳繑鍥炰竴涓猺esponse瀵硅薄锛屽叾瀛樺偍浜嗘湇鍔″櫒鍝嶅簲鐨勫唴瀹癸紝濡備笂瀹炰緥涓凡缁忔彁鍒扮殑r.text銆乺.status_code鈥︹��

鑾峰彇鏂囨湰鏂瑰紡鐨勫搷搴斾綋瀹炰緥锛氬綋浣犺闂� r.text 涔嬫椂锛屼細浣跨敤鍏跺搷搴旂殑鏂囨湰缂栫爜杩涜瑙g爜锛屽苟涓斾綘鍙互淇敼鍏剁紪鐮佽 r.text 浣跨敤鑷畾涔夌殑缂栫爜杩涜瑙g爜銆�

r = requests.get('http://www.itwhy.org')print(r.text,'\n{}\n'.format('*'*79),r.encoding)

r.encoding='GBK'print(r.text,'\n{}\n'.format('*'*79),r.encoding)

鍏朵粬鍝嶅簲锛�

r.status_code #鍝嶅簲鐘舵�佺爜

r.raw #杩斿洖鍘熷鍝嶅簲浣擄紝涔熷氨鏄痷rllib鐨剅esponse瀵硅薄锛屼娇鐢╮.raw.read()璇诲彇

r.content #瀛楄妭鏂瑰紡鐨勫搷搴斾綋锛屼細鑷姩涓轰綘瑙g爜gzip鍜宒eflate鍘嬬缉

r.text #瀛楃涓叉柟寮忕殑鍝嶅簲浣擄紝浼氳嚜鍔ㄦ牴鎹搷搴斿ご閮ㄧ殑瀛楃缂栫爜杩涜瑙g爜

r.headers #浠ュ瓧鍏稿璞″瓨鍌ㄦ湇鍔″櫒鍝嶅簲澶达紝浣嗘槸杩欎釜瀛楀吀姣旇緝鐗规畩锛屽瓧鍏搁敭涓嶅尯鍒嗗ぇ灏忓啓锛岃嫢閿笉瀛樺湪鍒欒繑鍥濶one

#*鐗规畩鏂规硶*#

r.json()#Requests涓唴缃殑JSON瑙g爜鍣�

r.raise_for_status()#澶辫触璇锋眰(闈�200鍝嶅簲)鎶涘嚭寮傚父

妗堜緥涔嬩竴锛�

importrequests

URL='http://ip.taobao.com/service/getIpInfo.php'#娣樺疂IP鍦板潃搴揂PItry:

r= requests.get(URL,params={'ip':'8.8.8.8'},timeout=1)

r.raise_for_status()#濡傛灉鍝嶅簲鐘舵�佺爜涓嶆槸200锛屽氨涓诲姩鎶涘嚭寮傚父exceptrequests.RequestException as e:print(e)else:

result=r.json()print(type(result),result,sep='\n')

鍥涖�佷笂浼犳枃浠�

浣跨敤 Requests 妯″潡锛屼笂浼犳枃浠朵篃鏄姝ょ畝鍗曠殑锛屾枃浠剁殑绫诲瀷浼氳嚜鍔ㄨ繘琛屽鐞嗭細

importrequests

url='http://127.0.0.1:5000/upload'

files= {'file': open('/home/lyb/sjzl.mpg','rb')

}#files = {'file':('report.jpg',open('/home/lyb/sjzl.mpg','rb'))}聽 聽 #鏄惧紡鐨勮缃枃浠跺悕

r= requests.post(url,files=files)print(r.text)

鏇村姞鏂逛究鐨勬槸锛屼綘鍙互鎶婂瓧绗︿覆褰撶潃鏂囦欢杩涜涓婁紶锛�

importrequests

url='http://127.0.0.1:5000/upload'

files= {'file':('test.txt',b'Hello Requests.')}#蹇呴渶鏄惧紡鐨勮缃枃浠跺悕

r= requests.post(url,files=files)print(r.text)

浜斻�佽韩浠介獙璇�

鍩烘湰韬唤璁よ瘉(HTTP Basic Auth):

import requests

from requests.auth import HTTPBasicAuth

r= requests.get('https://httpbin.org/hidden-basic-auth/user/passwd',auth=HTTPBasicAuth('user','passwd'))

#r = requests.get('https://httpbin.org/hidden-basic-auth/user/passwd',auth=('user','passwd'))#绠�鍐檖rint(r.json())

鍙︿竴绉嶉潪甯告祦琛岀殑HTTP韬唤璁よ瘉褰㈠紡鏄憳瑕佸紡韬唤璁よ瘉锛孯equests瀵瑰畠鐨勬敮鎸佷篃鏄紑绠卞嵆鍙敤鐨�:

requests.get(URL,auth=HTTPDigestAuth('user','pass'))

鍏�丆ookies涓庝細璇濆璞�

濡傛灉鏌愪釜鍝嶅簲涓寘鍚竴浜汣ookie锛屼綘鍙互蹇�熻闂畠浠細

import requests

r= requests.get('http://www.google.com.hk/')

print(r.cookies['NID'])

print(tuple(r.cookies))

瑕佹兂鍙戦�佷綘鐨刢ookies鍒版湇鍔″櫒锛屽彲浠ヤ娇鐢� cookies 鍙傛暟锛�

import requests

url='http://httpbin.org/cookies'cookies= {'testCookies_1':'Hello_Python3','testCookies_2':'Hello_Requests'}#鍦–ookie Version 0涓瀹氱┖鏍笺�佹柟鎷彿銆佸渾鎷彿銆佺瓑浜庡彿銆侀�楀彿銆佸弻寮曞彿銆佹枩鏉犮�侀棶鍙枫�丂锛屽啋鍙凤紝鍒嗗彿绛夌壒娈婄鍙烽兘涓嶈兘浣滀负Cookie鐨勫唴瀹广��

r = requests.get(url,cookies=cookies)

print(r.json())

浼氳瘽瀵硅薄璁╀綘鑳藉璺ㄨ姹備繚鎸佹煇浜涘弬鏁帮紝鏈�鏂逛究鐨勬槸鍦ㄥ悓涓�涓猄ession瀹炰緥鍙戝嚭鐨勬墍鏈夎姹備箣闂翠繚鎸乧ookies锛屼笖杩欎簺閮芥槸鑷姩澶勭悊鐨勶紝鐢氭槸鏂逛究銆�

涓嬮潰灏辨潵涓�涓湡姝g殑瀹炰緥锛屽涓嬫槸蹇洏绛惧埌鑴氭湰锛�

import requests

headers= {'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8','Accept-Encoding':'gzip,deflate,compress','Accept-Language':'en-us;q=0.5,en;q=0.3','Cache-Control':'max-age=0','Connection':'keep-alive','User-Agent':'Mozilla/5.0(X11;Ubuntu;Linux x86_64;rv:22.0)Gecko/20100101 Firefox/22.0'}

s=requests.Session()

s.headers.update(headers)#s.auth =('superuser','123')

s.get('https://www.kuaipan.cn/account_login.htm')

_URL='http://www.kuaipan.cn/index.php's.post(_URL,params={'ac':'account','op':'login'},

data={'username':'****@foxmail.com','userpwd':'********','isajax':'yes'})

r= s.get(_URL,params={'ac':'zone','op':'taskdetail'})print(r.json())

s.get(_URL,params={'ac':'common','op':'usersign'})

涓冦�佽秴鏃朵笌寮傚父

timeout 浠呭杩炴帴杩囩▼鏈夋晥锛屼笌鍝嶅簲浣撶殑涓嬭浇鏃犲叧銆�

>>> requests.get('http://github.com',timeout=0.001)

Traceback(most recent call last):

File"",line 1,inrequests.exceptions.Timeout: HTTPConnectionPool(host='github.com',port=80): Request timed out.(timeout=0.001)

鎵�鏈塕equests鏄惧紡鎶涘嚭鐨勫紓甯搁兘缁ф壙鑷猺equests.exceptions.RequestException锛欳onnectionError銆丠TTPError銆乀imeout銆乀ooManyRedirects銆�

requests鏄痯ython鐨勪竴涓狧TTP瀹㈡埛绔簱锛岃窡urllib锛寀rllib2绫讳技锛岄偅涓轰粈涔堣鐢╮equests鑰屼笉鐢╱rllib2鍛紵瀹樻柟鏂囨。涓槸杩欐牱璇存槑鐨勶細

python鐨勬爣鍑嗗簱urllib2鎻愪緵浜嗗ぇ閮ㄥ垎闇�瑕佺殑HTTP鍔熻兘锛屼絾鏄疉PI澶�嗗ぉ浜嗭紝涓�涓畝鍗曠殑鍔熻兘灏遍渶瑕佷竴澶у爢浠g爜銆�

鎻掓挱涓ソ娑堟伅锛佸垰鐪嬪埌requests鏈変簡涓枃缈昏瘧鐗堬紝寤鸿鑻辨枃涓嶅ソ鐨勭湅鐪嬶紝鍐呭涔熸瘮鎴戠殑鍗氬濂藉浜嗭紝鍏蜂綋閾炬帴鏄細http://cn.python-requests.org/en/latest/銆�

1.瀹夎

褰撶劧锛屾湁easy_install鎴杙ip鐨勬湅鍙嬪彲浠ョ洿鎺ヤ娇鐢細easy_install requests鎴栬�卲ip install requests鏉ュ畨瑁呫��

娴嬭瘯锛氬湪IDLE涓緭鍏mport requests锛屽鏋滄病鎻愮ず閿欒锛岄偅璇存槑宸茬粡瀹夎鎴愬姛浜嗭紒

2.灏忚瘯鐗涘垁

>>>importrequests>>>r=requests.get('http://www.zhidaow.com')#鍙戦�佽姹�>>>r.status_code#杩斿洖鐮�200>>>r.headers['content-type']#杩斿洖澶撮儴淇℃伅'text/html;charset=utf8'>>>r.encoding#缂栫爜淇℃伅'utf-8'>>>r.text#鍐呭閮ㄥ垎锛圥S锛岀敱浜庣紪鐮侀棶棰橈紝寤鸿杩欓噷浣跨敤r.content锛塽'\n

鏄笉鏄緢绠�鍗曪紵姣攗rllib2鍜寀rllib绠�鍗曠洿瑙傜殑澶氾紵锛侀偅璇锋帴鐫�鐪嬪揩閫熸寚鍗楀惂銆�

3. 蹇�熸寚鍗�

3.1鍙戦�佽姹�

鍙戦�佽姹傚緢绠�鍗曠殑锛岄鍏堣瀵煎叆requests妯″潡锛�

>>>import requests

鎺ヤ笅鏉ヨ鎴戜滑鑾峰彇涓�涓綉椤碉紝渚嬪鐧惧害棣栭〉锛�

>>>r=requests.get('http://www.baidu.com')

鎺ヤ笅鏉ワ紝鎴戜滑灏卞彲浠ヤ娇鐢ㄨ繖涓猺鐨勫悇绉嶆柟娉曞拰鍑芥暟浜嗐��

鍙﹀锛孒TTP璇锋眰杩樻湁寰堝绫诲瀷锛屾瘮濡侾OST,PUT,DELETE,HEAD,OPTIONS銆備篃閮藉彲浠ョ敤鍚屾牱鐨勬柟寮忓疄鐜帮細

>>>r=requests.post("http://httpbin.org/post")>>>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")

鍥犱负鐩墠鎴戣繕娌$敤鍒拌繖浜涳紝鎵�浠ユ病鏈夋繁鍏ョ爺绌躲��

3.2鍦║RLs涓紶閫掑弬鏁�

鏈夋椂鍊欐垜浠渶瑕佸湪URL涓紶閫掑弬鏁帮紝姣斿鍦ㄩ噰闆嗙櫨搴︽悳绱㈢粨鏋滄椂锛屾垜浠瑆d鍙傛暟锛堟悳绱㈣瘝锛夊拰rn鍙傛暟锛堟悳绱犵粨鏋滄暟閲忥級锛屼綘鍙互鎵嬪伐缁勬垚URL锛屾垨鑰呬娇鐢細

r=requests.get(鈥渉ttp://www.baidu.com/鈥�,params={鈥渨d鈥�:鈥漷est鈥潁)銆�

3.3 鑾峰彇鍝嶅簲鍐呭

鍙互閫氳繃r.text鏉ヨ幏鍙栫綉椤电殑鍐呭銆�

>>>r=requests.get('https://www.zhidaow.com')>>>r.textu'\n

鏂囨。閲岃锛宺equests浼氳嚜鍔ㄥ皢鍐呭杞爜銆傚ぇ澶氭暟unicode瀛椾綋閮戒細鏃犵紳杞爜銆備絾鎴戝湪cygwin涓嬩娇鐢ㄦ椂鑰佹槸鍑虹幇UnicodeEncodeError閿欒锛岄儊闂枫�傚�掓槸鍦╬ython鐨処DLE涓畬鍏ㄦ甯搞��

鍙﹀锛岃繕鍙互閫氳繃r.content鏉ヨ幏鍙栭〉闈㈠唴瀹广��

>>>r=requests.get('https://www.zhidaow.com')>>>r.contentb'\n

鏂囨。涓r.content鏄互瀛楄妭鐨勬柟寮忓幓鏄剧ず锛屾墍浠ュ湪IDLE涓互b寮�澶淬�備絾鎴戝湪cygwin涓敤璧锋潵骞舵病鏈夛紝涓嬭浇缃戦〉姝eソ銆傛墍浠ュ氨鏇夸唬浜唘rllib2鐨剈rllib2.urlopen(url).read()鍔熻兘銆傦紙鍩烘湰涓婃槸鎴戠敤鐨勬渶澶氱殑涓�涓姛鑳姐�傦級

3.4 鑾峰彇缃戦〉缂栫爜

鍙互浣跨敤r.encoding鏉ヨ幏鍙栫綉椤电紪鐮併��

>>>r=requests.get('http://www.baidu.com')>>>r.encoding'utf-8'

褰撲綘鍙戦�佽姹傛椂锛宺equests浼氭牴鎹瓾TTP澶撮儴鏉ョ寽娴嬬綉椤电紪鐮侊紝褰撲綘浣跨敤r.text鏃讹紝requests灏变細浣跨敤杩欎釜缂栫爜銆傚綋鐒朵綘杩樺彲浠ヤ慨鏀箁equests鐨勭紪鐮佸舰寮忋��

>>>r=requests.get('http://www.zhidaow.com')>>>r.encoding'utf-8'>>>r.encoding='ISO-8859-1'

鍍忎笂闈㈢殑渚嬪瓙锛屽encoding淇敼鍚庡氨鐩存帴浼氱敤淇敼鍚庣殑缂栫爜鍘昏幏鍙栫綉椤靛唴瀹广��

3.5 json

鍍弖rllib鍜寀rllib2锛屽鏋滅敤鍒癹son锛屽氨瑕佸紩鍏ユ柊妯″潡锛屽json鍜宻implejson锛屼絾鍦╮equests涓凡缁忔湁浜嗗唴缃殑鍑芥暟锛宺.json()銆傚氨鎷挎煡璇P鐨凙PI鏉ヨ锛�

>>>r=requests.get('http://ip.taobao.com/service/getIpInfo.php?ip=122.88.60.28')>>>r.json()['data']['country']'涓浗'

3.6 缃戦〉鐘舵�佺爜

鎴戜滑鍙互鐢╮.status_code鏉ユ鏌ョ綉椤电殑鐘舵�佺爜銆�

>>>r=requests.get('http://www.mengtiankong.com')>>>r.status_code200>>>r=requests.get('http://www.mengtiankong.com/123123/')>>>r.status_code404>>>r=requests.get('http://www.baidu.com/link?url=QeTRFOS7TuUQRppa0wlTJJr6FfIYI1DJprJukx4Qy0XnsDO_s9baoO8u1wvjxgqN')>>>r.urlu'http://www.zhidaow.com/>>>r.status_code200

鍓嶄袱涓緥瀛愬緢姝e父锛岃兘姝e父鎵撳紑鐨勮繑鍥�200锛屼笉鑳芥甯告墦寮�鐨勮繑鍥�404銆備絾绗笁涓氨鏈夌偣濂囨�簡锛岄偅涓槸鐧惧害鎼滅储缁撴灉涓殑302璺宠浆鍦板潃锛屼絾鐘舵�佺爜鏄剧ず鏄�200锛屾帴涓嬫潵鎴戠敤浜嗕竴鎷涜浠栧師褰㈡瘯闇诧細

>>>r.history(,)

杩欓噷鑳界湅鍑轰粬鏄娇鐢ㄤ簡302璺宠浆銆備篃璁告湁浜鸿涓鸿繖鏍峰彲浠ラ�氳繃鍒ゆ柇鍜屾鍒欐潵鑾峰彇璺宠浆鐨勭姸鎬佺爜浜嗭紝鍏跺疄杩樻湁涓洿绠�鍗曠殑鏂规硶锛�

>>>r=requests.get('http://www.baidu.com/link?url=QeTRFOS7TuUQRppa0wlTJJr6FfIYI1DJprJukx4Qy0XnsDO_s9baoO8u1wvjxgqN',allow_redirects=False)>>>r.status_code302

鍙鍔犱笂涓�涓弬鏁癮llow_redirects锛岀姝簡璺宠浆锛屽氨鐩存帴鍑虹幇璺宠浆鐨勭姸鎬佺爜浜嗭紝濂界敤鍚э紵鎴戜篃鍒╃敤杩欎釜鍦ㄦ渶鍚庝竴鎺屽仛浜嗕釜绠�鍗曠殑鑾峰彇缃戦〉鐘舵�佺爜鐨勫皬搴旂敤锛屽師鐞嗗氨鏄繖涓��

3.7 鍝嶅簲澶村唴瀹�

鍙互閫氳繃r.headers鏉ヨ幏鍙栧搷搴斿ご鍐呭銆�

>>>r=requests.get('http://www.zhidaow.com')>>>r.headers{'content-encoding':'gzip','transfer-encoding':'chunked','content-type':'text/html;charset=utf-8';...}

鍙互鐪嬪埌鏄互瀛楀吀鐨勫舰寮忚繑鍥炰簡鍏ㄩ儴鍐呭锛屾垜浠篃鍙互璁块棶閮ㄥ垎鍐呭銆�

>>>r.headers['Content-Type']'text/html;charset=utf-8'>>>r.headers.get('content-type')'text/html;charset=utf-8'

3.8 璁剧疆瓒呮椂鏃堕棿

鎴戜滑鍙互閫氳繃timeout灞炴�ц缃秴鏃舵椂闂达紝涓�鏃﹁秴杩囪繖涓椂闂磋繕娌¤幏寰楀搷搴斿唴瀹癸紝灏变細鎻愮ず閿欒銆�

>>>requests.get('http://github.com',timeout=0.001)Traceback(mostrecentcalllast):File"",line1,inrequests.exceptions.Timeout:HTTPConnectionPool(host='github.com',port=80):Requesttimedout.(timeout=0.001)

3.9浠g悊璁块棶

閲囬泦鏃朵负閬垮厤琚皝IP锛岀粡甯镐細浣跨敤浠g悊銆俽equests涔熸湁鐩稿簲鐨刾roxies灞炴�с��

importrequestsproxies={"http":"http://10.10.1.10:3128","https":"http://10.10.1.10:1080",}requests.get("http://www.zhidaow.com",proxies=proxies)

濡傛灉浠g悊闇�瑕佽处鎴峰拰瀵嗙爜锛屽垯闇�杩欐牱锛�

proxies={"http":"http://user:[email protected]:3128/",}

3.10璇锋眰澶村唴瀹�

璇锋眰澶村唴瀹瑰彲浠ョ敤r.request.headers鏉ヨ幏鍙栥��

>>>r.request.headers{'Accept-Encoding':'identity,deflate,compress,gzip','Accept':'*/*','User-Agent':'python-requests/1.2.3 CPython/2.7.3 Windows/XP'}

3.11 鑷畾涔夎姹傚ご閮�

浼璇锋眰澶撮儴鏄噰闆嗘椂缁忓父鐢ㄧ殑锛屾垜浠彲浠ョ敤杩欎釜鏂规硶鏉ラ殣钘忥細

r=requests.get('http://www.zhidaow.com')printr.request.headers['User-Agent']#python-requests/1.2.3 CPython/2.7.3 Windows/XPheaders={'User-Agent':'alexkh'}r=requests.get('http://www.zhidaow.com',headers=headers)printr.request.headers['User-Agent']#alexkh

3.12鎸佷箙杩炴帴keep-alive

requests鐨刱eep-alive鏄熀浜巙rllib3锛屽悓涓�浼氳瘽鍐呯殑鎸佷箙杩炴帴瀹屽叏鏄嚜鍔ㄧ殑銆傚悓涓�浼氳瘽鍐呯殑鎵�鏈夎姹傞兘浼氳嚜鍔ㄤ娇鐢ㄦ伆褰撶殑杩炴帴銆�

涔熷氨鏄锛屼綘鏃犻渶浠讳綍璁剧疆锛宺equests浼氳嚜鍔ㄥ疄鐜発eep-alive銆�

4. 绠�鍗曞簲鐢�

4.1 鑾峰彇缃戦〉杩斿洖鐮�

defget_status(url):r=requests.get(url,allow_redirects=False)returnr.status_codeprintget_status('http://www.zhidaow.com')#200printget_status('http://www.zhidaow.com/hi404/')#404printget_status('http://mengtiankong.com')#301printget_status('http://www.baidu.com/link?url=QeTRFOS7TuUQRppa0wlTJJr6FfIYI1DJprJukx4Qy0XnsDO_s9baoO8u1wvjxgqN')#302printget_status('http://www.huiya56.com/com8.intre.asp?46981.html')#500

瀹樻柟鏂囨。

requests鐨勫叿浣撳畨瑁呰繃绋嬭鐪嬶細http://docs.python-requests.org/en/latest/user/install.html#install

requests鐨勫畼鏂规寚鍗楁枃妗o細http://docs.python-requests.org/en/latest/user/quickstart.html

requests鐨勯珮绾ф寚鍗楁枃妗o細http://docs.python-requests.org/en/latest/user/advanced.html#advanced

你可能感兴趣的:(Python鍩虹瀛︿範涔媟equests鍩虹(涓�))