1.鍑嗗宸ヤ綔

棣栧厛鍛紝鎴戜滑瑕佺‘淇濇垜浠凡缁忎箣鍓嶅畨瑁卹equests搴擄紝濡傛灉娌℃湁瀹夎锛屾寜鐓т笅闈㈡楠ゆ寜鐓у簱銆

pip 瀹夎

鏃犺鏄 Windows銆丩inux 杩樻槸 Mac锛岄兘鍙互閫氳繃 pip 杩欎釜鍖呯鐞嗗伐鍏锋潵瀹夎銆

鍦ㄥ懡浠よ涓嬭繍琛屽涓嬪懡浠ゅ嵆鍙畬鎴 requests 搴撶殑瀹夎锛

1

pip3 install requests

杩欐槸鏈绠鍗曠殑瀹夎鏂瑰紡锛屾帹鑽愭绉嶆柟娉曞畨瑁呫

楠岃瘉瀹夎

涓轰簡楠岃瘉搴撴槸鍚﹀凡缁忓畨瑁呮垚鍔燂紝鍙互鍦ㄥ懡浠よ涓嬫祴璇曚竴涓嬶細

1

2

3

4

5

importrequests

  

res=requests.get('https://www.baidu.com/')

  

print(res)

杈撳叆濡備笂鍐呭锛屽鏋滀粈涔堥敊璇彁绀轰篃娌℃湁锛岄偅涔堝氨璇佹槑鎴戜滑宸茬粡鎴愬姛瀹夎浜 requests銆

2.瀹炰緥寮曞叆

requests搴撹姹傜綉椤电敤鍒扮殑鏄痝et()鏂规硶锛屼笅闈㈤氳繃瀹炰緥鏉ョ湅涓涓嬨

1

2

3

4

5

6

7

8

9

10

11

importrequests

  

res=requests.get('https://www.baidu.com/')

  

print(type(res))

  

print(res)

  

print(res.text)

  

print(res.cookies)

杩欓噷鎴戜滑璋冪敤get()鏂规硶瀹炵幇锛屽緱鍒颁竴涓猺esponse瀵硅薄锛岀劧鍚庡垎鍒緭鍑簉esponse鐨勭被鍨嬶紝鐘舵佺爜锛屽唴瀹逛互鍙奵ookies銆

鍙兘鎴愬姛鍦颁娇鐢╣et()鏂规硶鍙戣捣涓涓猤et()璇锋眰骞朵笉瓒充负濂囷紝杩樻湁鍏朵粬鏇存柟渚跨殑璇锋眰鍙緵浣跨敤銆傛瘮濡俻ost(),put()绛夌瓑銆

3.get()璇锋眰

HTTP鏈甯歌鐨勮姹備箣涓灏辨槸GET璇锋眰锛屼笅闈㈡垜浠鍏堝厛鏉ヤ簡瑙d竴涓嬪埄鐢╮equests鏋勫缓GET鐨勬柟娉

鍩烘湰瀹炰緥

棣栧厛锛屾垜浠瀯寤轰竴涓渶绠鍗曠殑get璇锋眰锛岃姹傜殑閾炬帴濡備笅锛岃缃戠珯浼氬垽鏂鏋滅敤鎴峰彂璧风殑鐨勬槸get璇锋眰鐨勮瘽锛屽畠灏变細杩斿洖鍝嶅簲鐨勮姹備俊鎭

1

2

3

4

5

importrequests

  

res=requests.get('http://httpbin.org/get')

  

print(res.text)

杩愯鐨勭粨鏋滃涓嬶細

1

2

3

4

5

6

7

8

9

10

11

12

{

  "args": {},

  "headers": {

    "Accept":"*/*",

    "Accept-Encoding":"gzip, deflate",

    "Host":"httpbin.org",

    "User-Agent":"python-requests/2.27.1",

    "X-Amzn-Trace-Id":"Root=1-637ae5d7-35da1bf57b139d152585d12a"

  },

  "origin":"223.215.67.113",

  "url":"http://httpbin.org/get"

}

鍙互鍙戠幇锛屾垜浠垚鍔熷彂璧蜂簡get璇锋眰锛岃繑鍥炵粨鏋滀腑鍖呭惈璇锋眰澶达紝url锛孖P绛変俊鎭

閭d箞锛屽浜嶨ET璇锋眰锛屽鏋滄垜浠兂瑕侀檮鍔犻澶栦俊鎭紝涓鑸庝箞娣诲姞鍛紵姣斿璁诧紝鐜板湪鎯虫坊鍔犱袱涓弬鏁帮紝鍏朵腑name鏄疶ina锛宎ge鏄18銆傝鏋勯犺繖涓姹傞摼鎺ワ紝鏄笉鏄彲浠ョ洿鎺ュ啓鎴愶細

1

r=requests.get('http://httpbin.org/get?name=Tina&age=18')

杩欐牱涔熸槸鍙互鐨勶紝鎴戜滑杩樺彲浠ラ氳繃瀛楀吀鏉ユ瀯閫犮傚埄鐢╬arams杩欎釜鍙傛暟灏卞ソ浜嗐

1

2

3

4

5

6

7

8

9

10

11

12

importrequests

  

data={

  

    'name':'Tina'锛

     

    'age':'18'

    }

  

res=requests.get('http://httpbin.org/get',params=data)

  

print(res.text)

杩愯缁撴灉濡備笅锛

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

{

  "args": {

    "age":"18",

    "name":"Tina"

  },

  "headers": {

    "Accept":"*/*",

    "Accept-Encoding":"gzip, deflate",

    "Host":"httpbin.org",

    "User-Agent":"python-requests/2.27.1",

    "X-Amzn-Trace-Id":"Root=1-637ae902-695483e87b26b3ad49d15df7"

  },

  "origin":"223.215.67.113",

  "url":"http://httpbin.org/get?name=Tina&age=18"

}

閫氳繃杩愯缁撴灉鍒ゆ柇锛岃姹傜殑閾炬帴鑷姩鍙樻垚浜嗗甫鏈夊悗缂鐨勯摼鎺ャ

姝ゅ锛屽疄闄呬笂缃戦〉杩斿洖鐨勬槸瀛楃涓茬被鍨嬶紙str锛夛紝浣嗘槸鍏舵牸寮忎负json()锛屾垜浠彲浠ヤ娇鐢╦son鏉ヨ繑鍥炰竴涓瓧鍏搞傚鏋滀笉鏄痡son鏍煎紡锛屼娇鐢╦son灏变細鎶ラ敊锛屾姏鍑簀son.decoder.JSONDecodeError寮傚父銆

4.post()璇锋眰

闄や簡鏈鍩烘湰鐨刧et璇锋眰锛岃繕鏈変竴绉嶆瘮杈冨父瑙佺殑璇锋眰鏂瑰紡鏄痯ost().銆備娇鐢╮equests瀹炵幇post璇锋眰涔熸槸闈炲父绠鍗曪紝绀轰緥濡備笅銆

1

2

3

4

5

importrequests

  

res=requests.post('http://httpbin.org/post')

  

print(res.text)

杩愯涔嬪悗灏变細鍙戠幇寰楀埌浜嗙粨鏋滐紝灏辫鏄庢垜浠琾ost璇锋眰鎴愬姛銆

5.鍝嶅簲

鍙戦佽姹傦紝涔嬪悗寰楀埌鐨勮偗瀹氬氨鏄搷搴斻傞櫎浜唗ext锛岃繕鏈夌姸鎬佺爜锛屽搷搴斿ご锛宑ookies绛夌瓑銆

requests 搴撳彲浠ョ敤鏉ュ彂閫 HTTP 璇锋眰骞惰幏鍙栧搷搴斻傚彂閫 HTTP 璇锋眰鍚庯紝浠庢湇鍔″櫒杩斿洖鐨勬墍鏈夋暟鎹兘浼氬寘鍚湪 Response 瀵硅薄涓俁esponse 瀵硅薄鏈変互涓嬪睘鎬э細

status_code锛欻TTP 鐘舵佺爜锛岃〃绀烘湇鍔″櫒鐨勫搷搴旂姸鎬併
headers锛氫竴涓瓧鍏革紝鍏朵腑鍖呭惈浜嗕粠鏈嶅姟鍣ㄨ繑鍥炵殑鎵鏈夊ご閮ㄤ俊鎭
body锛氫竴涓瓧鑺備覆锛屽寘鍚簡浠庢湇鍔″櫒杩斿洖鐨勬墍鏈夋暟鎹


浠ヤ笂灏辨槸Python涔媟equests瀹夎鏂规硶鐨勮缁嗗唴瀹癸紝鏇村淇℃伅璇峰叧娉∣D浜戝叾瀹冪浉鍏虫枃绔狅紒



鏈枃URL锛http://www.odweb.cn/news_show.html?id=170