Python缃戠粶鐖櫕鍩虹妯″潡 - requests妯″潡璇﹁В

1銆佹ā鍧楄鏄

requests鏄娇鐢ˋpache2 licensed 璁稿彲璇佺殑HTTP搴撱

鐢╬ython缂栧啓銆

姣攗rllib2妯″潡鏇寸畝娲併

Request鏀寔HTTP杩炴帴淇濇寔鍜岃繛鎺ユ睜锛屾敮鎸佷娇鐢╟ookie淇濇寔浼氳瘽锛屾敮鎸佹枃浠朵笂浼狅紝鏀寔鑷姩鍝嶅簲鍐呭鐨勭紪鐮侊紝鏀寔鍥介檯鍖栫殑URL鍜孭OST鏁版嵁鑷姩缂栫爜銆

鍦╬ython鍐呯疆妯″潡鐨勫熀纭涓婅繘琛屼簡楂樺害鐨勫皝瑁咃紝浠庤屼娇寰梡ython杩涜缃戠粶璇锋眰鏃讹紝鍙樺緱浜烘у寲锛屼娇鐢≧equests鍙互杞昏屾槗涓剧殑瀹屾垚娴忚鍣ㄥ彲鏈夌殑浠讳綍鎿嶄綔銆

鐜颁唬锛屽浗闄呭寲锛屽弸濂姐

requests浼氳嚜鍔ㄥ疄鐜版寔涔呰繛鎺eep-alive

2銆佸熀纭鍏ラ棬

1锛夊鍏ユā鍧

import requests

2锛夊彂閫佽姹傜殑绠娲

  绀轰緥浠g爜锛氳幏鍙栦竴涓綉椤碉紙涓汉github锛

import requests

r = requests.get('https://github.com/Ranxf') # 鏈鍩烘湰鐨勪笉甯﹀弬鏁扮殑get璇锋眰 r1 = requests.get(url='http://dict.baidu.com/s', params={'wd': 'python'}) # 甯﹀弬鏁扮殑get璇锋眰

鎴戜滑灏卞彲浠ヤ娇鐢ㄨ鏂瑰紡浣跨敤浠ヤ笅鍚勭鏂规硶

1   requests.get(鈥榟ttps://github.com/timeline.json鈥) # GET璇锋眰 2   requests.post(鈥渉ttp://httpbin.org/post鈥) # POST璇锋眰 3   requests.put(鈥渉ttp://httpbin.org/put鈥) # PUT璇锋眰 4   requests.delete(鈥渉ttp://httpbin.org/delete鈥) # DELETE璇锋眰 5   requests.head(鈥渉ttp://httpbin.org/get鈥) # HEAD璇锋眰 6   requests.options(鈥渉ttp://httpbin.org/get鈥 ) # OPTIONS璇锋眰

3锛変负url浼犻掑弬鏁

>>> url_params = {'key':'value'} # 瀛楀吀浼犻掑弬鏁帮紝濡傛灉鍊间负None鐨勯敭涓嶄細琚坊鍔犲埌url涓 >>> r = requests.get('your url',params = url_params) >>> print(r.url)
  your url?key=value

4锛夊搷搴旂殑鍐呭


r.encoding #鑾峰彇褰撳墠鐨勭紪鐮 r.encoding = 'utf-8' #璁剧疆缂栫爜 r.text #浠ncoding瑙f瀽杩斿洖鍐呭銆傚瓧绗︿覆鏂瑰紡鐨勫搷搴斾綋锛屼細鑷姩鏍规嵁鍝嶅簲澶撮儴鐨勫瓧绗︾紪鐮佽繘琛岃В鐮併 r.content #浠ュ瓧鑺傚舰寮忥紙浜岃繘鍒讹級杩斿洖銆傚瓧鑺傛柟寮忕殑鍝嶅簲浣擄紝浼氳嚜鍔ㄤ负浣犺В鐮 gzip 鍜 deflate 鍘嬬缉銆  r.headers #浠ュ瓧鍏稿璞″瓨鍌ㄦ湇鍔″櫒鍝嶅簲澶达紝浣嗘槸杩欎釜瀛楀吀姣旇緝鐗规畩锛屽瓧鍏搁敭涓嶅尯鍒嗗ぇ灏忓啓锛岃嫢閿笉瀛樺湪鍒欒繑鍥濶one  r.status_code #鍝嶅簲鐘舵佺爜 r.raw #杩斿洖鍘熷鍝嶅簲浣擄紝涔熷氨鏄 urllib 鐨 response 瀵硅薄锛屼娇鐢 r.raw.read()  r.ok # 鏌ョ湅r.ok鐨勫竷灏斿间究鍙互鐭ラ亾鏄惁鐧婚檰鎴愬姛 #*鐗规畩鏂规硶*# r.json() #Requests涓唴缃殑JSON瑙g爜鍣紝浠son褰㈠紡杩斿洖,鍓嶆彁杩斿洖鐨勫唴瀹圭‘淇濇槸json鏍煎紡鐨勶紝涓嶇劧瑙f瀽鍑洪敊浼氭姏寮傚父 r.raise_for_status() #澶辫触璇锋眰(闈200鍝嶅簲)鎶涘嚭寮傚父

post鍙戦乯son璇锋眰锛

1 import requests 2 import json 3  
4 r = requests.post('https://api.github.com/some/endpoint', data=json.dumps({'some': 'data'})) 5 print(r.json())

5锛夊畾鍒跺ご鍜宑ookie淇℃伅

header = {'user-agent': 'my-app/0.0.1''} cookie = {'key':'value'}
 r = requests.get/post('your url',headers=header,cookies=cookie) 
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)

6锛夊搷搴旂姸鎬佺爜

浣跨敤requests鏂规硶鍚庯紝浼氳繑鍥炰竴涓猺esponse瀵硅薄锛屽叾瀛樺偍浜嗘湇鍔″櫒鍝嶅簲鐨勫唴瀹癸紝濡備笂瀹炰緥涓凡缁忔彁鍒扮殑 r.text銆乺.status_code鈥︹
鑾峰彇鏂囨湰鏂瑰紡鐨勫搷搴斾綋瀹炰緥锛氬綋浣犺闂 r.text 涔嬫椂锛屼細浣跨敤鍏跺搷搴旂殑鏂囨湰缂栫爜杩涜瑙g爜锛屽苟涓斾綘鍙互淇敼鍏剁紪鐮佽 r.text 浣跨敤鑷畾涔夌殑缂栫爜杩涜瑙g爜銆

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

绀轰緥浠g爜锛


1 import requests 2 
3 r = requests.get('https://github.com/Ranxf') # 鏈鍩烘湰鐨勪笉甯﹀弬鏁扮殑get璇锋眰 4 print(r.status_code) # 鑾峰彇杩斿洖鐘舵 5 r1 = requests.get(url='http://dict.baidu.com/s', params={'wd': 'python'}) # 甯﹀弬鏁扮殑get璇锋眰 6 print(r1.url) 7 print(r1.text) # 鎵撳嵃瑙g爜鍚庣殑杩斿洖鏁版嵁

杩愯缁撴灉锛

/usr/bin/python3.5 /home/rxf/python3_1000/1000/python3_server/python3_requests/demo1.py 200 http://dict.baidu.com/s?wd=python
鈥︹︹︹

Process finished with exit code 0
 r.status_code #濡傛灉涓嶆槸200锛屽彲浠ヤ娇鐢 r.raise_for_status() 鎶涘嚭寮傚父

7锛夊搷搴

r.headers #杩斿洖瀛楀吀绫诲瀷,澶翠俊鎭 r.requests.headers #杩斿洖鍙戦佸埌鏈嶅姟鍣ㄧ殑澶翠俊鎭 r.cookies #杩斿洖cookie r.history #杩斿洖閲嶅畾鍚戜俊鎭,褰撶劧鍙互鍦ㄨ姹傛槸鍔犱笂allow_redirects = false 闃绘閲嶅畾鍚

8锛夎秴鏃


r = requests.get('url',timeout=1) #璁剧疆绉掓暟瓒呮椂锛屼粎瀵逛簬杩炴帴鏈夋晥


9)浼氳瘽瀵硅薄锛岃兘澶熻法璇锋眰淇濇寔鏌愪簺鍙傛暟

s = requests.Session()
s.auth = ('auth','passwd')
s.headers = {'key':'value'}
r = s.get('url')
r1 = s.get('url1') 

10锛変唬鐞


proxies = {'http':'ip1','https':'ip2' }
requests.get('url',proxies=proxies)


姹囨伙細


# HTTP璇锋眰绫诲瀷 # get绫诲瀷 r = requests.get('https://github.com/timeline.json') # post绫诲瀷 r = requests.post("http://m.ctrip.com/post") # put绫诲瀷 r = requests.put("http://m.ctrip.com/put") # delete绫诲瀷 r = requests.delete("http://m.ctrip.com/delete") # head绫诲瀷 r = requests.head("http://m.ctrip.com/head") # options绫诲瀷 r = requests.options("http://m.ctrip.com/get") # 鑾峰彇鍝嶅簲鍐呭 print(r.content) #浠ュ瓧鑺傜殑鏂瑰紡鍘绘樉绀猴紝涓枃鏄剧ず涓哄瓧绗 print(r.text) #浠ユ枃鏈殑鏂瑰紡鍘绘樉绀 #URL浼犻掑弬鏁 payload = {'keyword': '棣欐腐', 'salecityid': '2'}
r = requests.get("http://m.ctrip.com/webapp/tourvisa/visa_list", params=payload) print锛坮.url锛 #绀轰緥涓篽ttp://m.ctrip.com/webapp/tourvisa/visa_list?salecityid=2&keyword=棣欐腐 #鑾峰彇/淇敼缃戦〉缂栫爜 r = requests.get('https://github.com/timeline.json') print 锛坮.encoding锛 #json澶勭悊 r = requests.get('https://github.com/timeline.json') print锛坮.json()锛 # 闇瑕佸厛import json  # 瀹氬埗璇锋眰澶 url = 'http://m.ctrip.com' headers = {'User-Agent' : 'Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 4 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19'}
r = requests.post(url, headers=headers) print 锛坮.request.headers) #澶嶆潅post璇锋眰 url = 'http://m.ctrip.com' payload = {'some': 'data'}
r = requests.post(url, data=json.dumps(payload)) #濡傛灉浼犻掔殑payload鏄痵tring鑰屼笉鏄痙ict锛岄渶瑕佸厛璋冪敤dumps鏂规硶鏍煎紡鍖栦竴涓 # post澶氶儴鍒嗙紪鐮佹枃浠 url = 'http://m.ctrip.com' files = {'file': open('report.xls', 'rb')}
r = requests.post(url, files=files) # 鍝嶅簲鐘舵佺爜 r = requests.get('http://m.ctrip.com') print(r.status_code) # 鍝嶅簲澶 r = requests.get('http://m.ctrip.com') print (r.headers) print (r.headers['Content-Type']) print (r.headers.get('content-type')) #璁块棶鍝嶅簲澶撮儴鍒嗗唴瀹圭殑涓ょ鏂瑰紡 # Cookies url = 'http://example.com/some/cookie/setting/url' r = requests.get(url)
r.cookies['example_cookie_name'] #璇诲彇cookies  url = 'http://m.ctrip.com/cookies' cookies = dict(cookies_are='working')
r = requests.get(url, cookies=cookies) #鍙戦乧ookies #璁剧疆瓒呮椂鏃堕棿 r = requests.get('http://m.ctrip.com', timeout=0.001) #璁剧疆璁块棶浠g悊 proxies = { "http": "http://10.10.1.10:3128", "https": "http://10.10.1.100:4444",
          }
r = requests.get('http://m.ctrip.com', proxies=proxies) #濡傛灉浠g悊闇瑕佺敤鎴峰悕鍜屽瘑鐮侊紝鍒欓渶瑕佽繖鏍凤細 proxies = { "http": "http://user:pass@10.10.1.10:3128/",
}


# HTTP璇锋眰绫诲瀷 # get绫诲瀷 r = requests.get('https://github.com/timeline.json') # post绫诲瀷 r = requests.post("http://m.ctrip.com/post") # put绫诲瀷 r = requests.put("http://m.ctrip.com/put") # delete绫诲瀷 r = requests.delete("http://m.ctrip.com/delete") # head绫诲瀷 r = requests.head("http://m.ctrip.com/head") # options绫诲瀷 r = requests.options("http://m.ctrip.com/get") # 鑾峰彇鍝嶅簲鍐呭 print(r.content) #浠ュ瓧鑺傜殑鏂瑰紡鍘绘樉绀猴紝涓枃鏄剧ず涓哄瓧绗 print(r.text) #浠ユ枃鏈殑鏂瑰紡鍘绘樉绀 #URL浼犻掑弬鏁 payload = {'keyword': '棣欐腐', 'salecityid': '2'}
r = requests.get("http://m.ctrip.com/webapp/tourvisa/visa_list", params=payload) print锛坮.url锛 #绀轰緥涓篽ttp://m.ctrip.com/webapp/tourvisa/visa_list?salecityid=2&keyword=棣欐腐 #鑾峰彇/淇敼缃戦〉缂栫爜 r = requests.get('https://github.com/timeline.json') print 锛坮.encoding锛 #json澶勭悊 r = requests.get('https://github.com/timeline.json') print锛坮.json()锛 # 闇瑕佸厛import json  # 瀹氬埗璇锋眰澶 url = 'http://m.ctrip.com' headers = {'User-Agent' : 'Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 4 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19'}
r = requests.post(url, headers=headers) print 锛坮.request.headers) #澶嶆潅post璇锋眰 url = 'http://m.ctrip.com' payload = {'some': 'data'}
r = requests.post(url, data=json.dumps(payload)) #濡傛灉浼犻掔殑payload鏄痵tring鑰屼笉鏄痙ict锛岄渶瑕佸厛璋冪敤dumps鏂规硶鏍煎紡鍖栦竴涓 # post澶氶儴鍒嗙紪鐮佹枃浠 url = 'http://m.ctrip.com' files = {'file': open('report.xls', 'rb')}
r = requests.post(url, files=files) # 鍝嶅簲鐘舵佺爜 r = requests.get('http://m.ctrip.com') print(r.status_code) # 鍝嶅簲澶 r = requests.get('http://m.ctrip.com') print (r.headers) print (r.headers['Content-Type']) print (r.headers.get('content-type')) #璁块棶鍝嶅簲澶撮儴鍒嗗唴瀹圭殑涓ょ鏂瑰紡 # Cookies url = 'http://example.com/some/cookie/setting/url' r = requests.get(url)
r.cookies['example_cookie_name'] #璇诲彇cookies  url = 'http://m.ctrip.com/cookies' cookies = dict(cookies_are='working')
r = requests.get(url, cookies=cookies) #鍙戦乧ookies #璁剧疆瓒呮椂鏃堕棿 r = requests.get('http://m.ctrip.com', timeout=0.001) #璁剧疆璁块棶浠g悊 proxies = { "http": "http://10.10.1.10:3128", "https": "http://10.10.1.100:4444",
          }
r = requests.get('http://m.ctrip.com', proxies=proxies) #濡傛灉浠g悊闇瑕佺敤鎴峰悕鍜屽瘑鐮侊紝鍒欓渶瑕佽繖鏍凤細 proxies = { "http": "http://user:pass@10.10.1.10:3128/",
}

3銆佺ず渚嬩唬鐮

GET璇锋眰


1 # 1銆佹棤鍙傛暟瀹炰緥 2   
 3 import requests 4   
 5 ret = requests.get('https://github.com/timeline.json') 6   
 7 print(ret.url) 8 print(ret.text) 9   
10   
11   
12 # 2銆佹湁鍙傛暟瀹炰緥 13   
14 import requests 15   
16 payload = {'key1': 'value1', 'key2': 'value2'} 17 ret = requests.get("http://httpbin.org/get", params=payload) 18   
19 print(ret.url) 20 print(ret.text)

POST璇锋眰


# 1銆佸熀鏈琍OST瀹炰緥 import requests
  
payload = {'key1': 'value1', 'key2': 'value2'}
ret = requests.post("http://httpbin.org/post", data=payload) print(ret.text) # 2銆佸彂閫佽姹傚ご鍜屾暟鎹疄渚 import requests import json
  
url = 'https://api.github.com/some/endpoint' payload = {'some': 'data'}
headers = {'content-type': 'application/json'}
  
ret = requests.post(url, data=json.dumps(payload), headers=headers) print(ret.text) print(ret.cookies)

璇锋眰鍙傛暟

璇锋眰鍙傛暟
鍙傛暟绀轰緥浠g爜

json璇锋眰锛


#! /usr/bin/python3 import requests import json class url_request(): def __init__(self): ''' init ''' if __name__ == '__main__':
    heard = {'Content-Type': 'application/json'}
    payload = {'CountryName': '涓浗', 'ProvinceName': '鍥涘窛鐪', 'L1CityName': 'chengdu', 'L2CityName': 'yibing', 'TownName': '', 'Longitude': '107.33393', 'Latitude': '33.157131', 'Language': 'CN'}
    r = requests.post("http://www.xxxxxx.com/CityLocation/json/LBSLocateCity", heards=heard, data=payload)
    data = r.json() if r.status_code!=200: print('LBSLocateCity API Error' + str(r.status_code)) print(data['CityEntities'][0]['CityID']) # 鎵撳嵃杩斿洖json涓殑鏌愪釜key鐨剉alue print(data['ResponseStatus']['Ack']) print(json.dump(data, indent=4, sort_keys=True, ensure_ascii=False)) # 鏍戝舰鎵撳嵃json锛宔nsure_ascii蹇呴』璁句负False鍚﹀垯涓枃浼氭樉绀轰负unicode

Xml璇锋眰锛


#! /usr/bin/python3 import requests class url_request(): def __init__(self): """init""" if __name__ == '__main__':
    heards = {'Content-type': 'text/xml'}
    XML = 'WeChatJSTicket.JobWS.Job.JobRefreshTicket,WeChatJSTicket.JobWSRUN1127.0.0.11false' url = 'http://jobws.push.mobile.xxxxxxxx.com/RefreshWeiXInTokenJob/RefreshService.asmx' r = requests.post(url=url, heards=heards, data=XML)
    data = r.text print(data)

鐘舵佸紓甯稿鐞


import requests

URL = 'http://ip.taobao.com/service/getIpInfo.php' # 娣樺疂IP鍦板潃搴揂PI try:
    r = requests.get(URL, params={'ip': '8.8.8.8'}, timeout=1)
    r.raise_for_status() # 濡傛灉鍝嶅簲鐘舵佺爜涓嶆槸 200锛屽氨涓诲姩鎶涘嚭寮傚父 except requests.RequestException as e: print(e) else:
    result = r.json() print(type(result), result, sep='\n')

涓婁紶鏂囦欢

浣跨敤request妯″潡锛屼篃鍙互涓婁紶鏂囦欢锛屾枃浠剁殑绫诲瀷浼氳嚜鍔ㄨ繘琛屽鐞嗭細


import requests
 
url = 'http://127.0.0.1:8080/upload' files = {'file': open('/home/rxf/test.jpg', 'rb')} #files = {'file': ('report.jpg', open('/home/lyb/sjzl.mpg', 'rb'))}     #鏄惧紡鐨勮缃枃浠跺悕  r = requests.post(url, files=files) print(r.text)

request鏇村姞鏂逛究鐨勬槸锛屽彲浠ユ妸瀛楃涓插綋浣滄枃浠惰繘琛屼笂浼狅細


import requests
 
url = 'http://127.0.0.1:8080/upload' files = {'file': ('test.txt', b'Hello Requests.')} #蹇呴渶鏄惧紡鐨勮缃枃浠跺悕  r = requests.post(url, files=files) print(r.text)

6) 韬唤楠岃瘉

鍩烘湰韬唤璁よ瘉(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'))    # 绠鍐 print(r.json())

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

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

Cookies涓庝細璇濆璞

濡傛灉鏌愪釜鍝嶅簲涓寘鍚竴浜汣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'})

requests妯″潡鎶撳彇缃戦〉婧愮爜骞朵繚瀛樺埌鏂囦欢绀轰緥

杩欐槸涓涓熀鏈殑鏂囦欢淇濆瓨鎿嶄綔锛屼絾杩欓噷鏈夊嚑涓煎緱娉ㄦ剰鐨勯棶棰橈細

1.瀹夎requests鍖咃紝鍛戒护琛岃緭鍏ip install requests鍗冲彲鑷姩瀹夎銆傚緢澶氫汉鎺ㄨ崘浣跨敤requests锛岃嚜甯︾殑urllib.request涔熷彲浠ユ姄鍙栫綉椤垫簮鐮

2.open鏂规硶encoding鍙傛暟璁句负utf-8锛屽惁鍒欎繚瀛樼殑鏂囦欢浼氬嚭鐜颁贡鐮併

3.濡傛灉鐩存帴鍦╟md涓緭鍑烘姄鍙栫殑鍐呭锛屼細鎻愮ず鍚勭缂栫爜閿欒锛屾墍浠ヤ繚瀛樺埌鏂囦欢鏌ョ湅銆

4.with open鏂规硶鏄洿濂界殑鍐欐硶锛屽彲浠ヨ嚜鍔ㄦ搷浣滃畬姣曞悗閲婃斁璧勬簮


#! /urs/bin/python3 import requests '''requests妯″潡鎶撳彇缃戦〉婧愮爜骞朵繚瀛樺埌鏂囦欢绀轰緥''' html = requests.get("http://www.baidu.com")
with open('test.txt', 'w', encoding='utf-8') as f:
    f.write(html.text) '''璇诲彇涓涓猼xt鏂囦欢锛屾瘡娆¤鍙栦竴琛岋紝骞朵繚瀛樺埌鍙︿竴涓猼xt鏂囦欢涓殑绀轰緥''' ff = open('testt.txt', 'w', encoding='utf-8')
with open('test.txt', encoding="utf-8") as f: for line in f:
        ff.write(line)
        ff.close()

鍥犱负鍦ㄥ懡浠よ涓墦鍗版瘡娆¤鍙栦竴琛岀殑鏁版嵁锛屼腑鏂囦細鍑虹幇缂栫爜閿欒锛屾墍浠ユ瘡娆¤鍙栦竴琛屽苟淇濆瓨鍒板彟涓涓枃浠讹紝杩欐牱鏉ユ祴璇曡鍙栨槸鍚︽甯搞傦紙娉ㄦ剰open鐨勬椂鍊欏埗瀹歟ncoding缂栫爜鏂瑰紡锛


鑷姩鐧婚檰"绀轰緥锛


#!/usr/bin/env python # -*- coding:utf-8 -*- import requests # ############## 鏂瑰紡涓 ############## """ # ## 1銆侀鍏堢櫥闄嗕换浣曢〉闈紝鑾峰彇cookie
i1 = requests.get(url="http://dig.chouti.com/help/service")
i1_cookies = i1.cookies.get_dict()

# ## 2銆佺敤鎴风櫥闄嗭紝鎼哄甫涓婁竴娆$殑cookie锛屽悗鍙板cookie涓殑 gpsd 杩涜鎺堟潈
i2 = requests.post(
    url="http://dig.chouti.com/login",
    data={
        'phone': "8615131255089",
        'password': "xxooxxoo",
        'oneMonth': ""
    },
    cookies=i1_cookies
)

# ## 3銆佺偣璧烇紙鍙渶瑕佹惡甯﹀凡缁忚鎺堟潈鐨刧psd鍗冲彲锛
gpsd = i1_cookies['gpsd']
i3 = requests.post(
    url="http://dig.chouti.com/link/vote?linksId=8589523",
    cookies={'gpsd': gpsd}
)

print(i3.text) """ # ############## 鏂瑰紡浜 ############## """ import requests

session = requests.Session()
i1 = session.get(url="http://dig.chouti.com/help/service")
i2 = session.post(
    url="http://dig.chouti.com/login",
    data={
        'phone': "8615131255089",
        'password': "xxooxxoo",
        'oneMonth': ""
    }
)
i3 = session.post(
    url="http://dig.chouti.com/link/vote?linksId=8589523"
)
print(i3.text) """


#!/usr/bin/env python # -*- coding:utf-8 -*- import requests from bs4 import BeautifulSoup # ############## 鏂瑰紡涓 ############## #
# # 1. 璁块棶鐧婚檰椤甸潰锛岃幏鍙 authenticity_token # i1 = requests.get('https://github.com/login') # soup1 = BeautifulSoup(i1.text, features='lxml') # tag = soup1.find(name='input', attrs={'name': 'authenticity_token'}) # authenticity_token = tag.get('value') # c1 = i1.cookies.get_dict() # i1.close() #
# # 1. 鎼哄甫authenticity_token鍜岀敤鎴峰悕瀵嗙爜绛変俊鎭紝鍙戦佺敤鎴烽獙璇 # form_data = { # "authenticity_token": authenticity_token, # "utf8": "", # "commit": "Sign in", # "login": "wupeiqi@live.com", # 'password': 'xxoo' # } #
# i2 = requests.post('https://github.com/session', data=form_data, cookies=c1) # c2 = i2.cookies.get_dict() # c1.update(c2) # i3 = requests.get('https://github.com/settings/repositories', cookies=c1) #
# soup3 = BeautifulSoup(i3.text, features='lxml') # list_group = soup3.find(name='div', class_='listgroup') #
# from bs4.element import Tag #
# for child in list_group.children: # if isinstance(child, Tag): # project_tag = child.find(name='a', class_='mr-1') # size_tag = child.find(name='small') # temp = "椤圭洰:%s(%s); 椤圭洰璺緞:%s" % (project_tag.get('href'), size_tag.string, project_tag.string, ) # print(temp) # ############## 鏂瑰紡浜 ############## # session = requests.Session() # # 1. 璁块棶鐧婚檰椤甸潰锛岃幏鍙 authenticity_token # i1 = session.get('https://github.com/login') # soup1 = BeautifulSoup(i1.text, features='lxml') # tag = soup1.find(name='input', attrs={'name': 'authenticity_token'}) # authenticity_token = tag.get('value') # c1 = i1.cookies.get_dict() # i1.close() #
# # 1. 鎼哄甫authenticity_token鍜岀敤鎴峰悕瀵嗙爜绛変俊鎭紝鍙戦佺敤鎴烽獙璇 # form_data = { # "authenticity_token": authenticity_token, # "utf8": "", # "commit": "Sign in", # "login": "wupeiqi@live.com", # 'password': 'xxoo' # } #
# i2 = session.post('https://github.com/session', data=form_data) # c2 = i2.cookies.get_dict() # c1.update(c2) # i3 = session.get('https://github.com/settings/repositories') #
# soup3 = BeautifulSoup(i3.text, features='lxml') # list_group = soup3.find(name='div', class_='listgroup') #
# from bs4.element import Tag #
# for child in list_group.children: # if isinstance(child, Tag): # project_tag = child.find(name='a', class_='mr-1') # size_tag = child.find(name='small') # temp = "椤圭洰:%s(%s); 椤圭洰璺緞:%s" % (project_tag.get('href'), size_tag.string, project_tag.string, ) # print(temp)


#!/usr/bin/env python # -*- coding:utf-8 -*- import time import requests from bs4 import BeautifulSoup

session = requests.Session()

i1 = session.get(
    url='https://www.zhihu.com/#signin',
    headers={ 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36',
    }
)

soup1 = BeautifulSoup(i1.text, 'lxml')
xsrf_tag = soup1.find(name='input', attrs={'name': '_xsrf'})
xsrf = xsrf_tag.get('value')

current_time = time.time()
i2 = session.get(
    url='https://www.zhihu.com/captcha.gif',
    params={'r': current_time, 'type': 'login'},
    headers={ 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36',
    })

with open('zhihu.gif', 'wb') as f:
    f.write(i2.content)

captcha = input('璇锋墦寮zhihu.gif鏂囦欢锛屾煡鐪嬪苟杈撳叆楠岃瘉鐮侊細')
form_data = { "_xsrf": xsrf, 'password': 'xxooxxoo', "captcha": 'captcha', 'email': '424662508@qq.com' }
i3 = session.post(
    url='https://www.zhihu.com/login/email',
    data=form_data,
    headers={ 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36',
    }
)

i4 = session.get(
    url='https://www.zhihu.com/settings/profile',
    headers={ 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36',
    }
)

soup4 = BeautifulSoup(i4.text, 'lxml')
tag = soup4.find(id='rename-section')
nick_name = tag.find('span',class_='name').string print(nick_name)


#!/usr/bin/env python # -*- coding:utf-8 -*- import re import json import base64 import rsa import requests def js_encrypt(text):
    b64der = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCp0wHYbg/NOPO3nzMD3dndwS0MccuMeXCHgVlGOoYyFwLdS24Im2e7YyhB0wrUsyYf0/nhzCzBK8ZC9eCWqd0aHbdgOQT6CuFQBMjbyGYvlVYU2ZP7kG9Ft6YV6oc9ambuO7nPZh+bvXH0zDKfi02prknrScAKC0XhadTHT3Al0QIDAQAB' der = base64.standard_b64decode(b64der)

    pk = rsa.PublicKey.load_pkcs1_openssl_der(der)
    v1 = rsa.encrypt(bytes(text, 'utf8'), pk)
    value = base64.encodebytes(v1).replace(b'\n', b'')
    value = value.decode('utf8') return value


session = requests.Session()

i1 = session.get('https://passport.cnblogs.com/user/signin')
rep = re.compile("'VerificationToken': '(.*)'")
v = re.search(rep, i1.text)
verification_token = v.group(1)

form_data = { 'input1': js_encrypt('wptawy'), 'input2': js_encrypt('asdfasdf'), 'remember': False
}

i2 = session.post(url='https://passport.cnblogs.com/user/signin',
                  data=json.dumps(form_data),
                  headers={ 'Content-Type': 'application/json; charset=UTF-8', 'X-Requested-With': 'XMLHttpRequest', 'VerificationToken': verification_token}
                  )

i3 = session.get(url='https://i.cnblogs.com/EditDiary.aspx') print(i3.text)

#!/usr/bin/env python # -*- coding:utf-8 -*- import requests # 绗竴姝ワ細璁块棶鐧婚檰椤,鎷垮埌X_Anti_Forge_Token锛孹_Anti_Forge_Code # 1銆佽姹倁rl:https://passport.lagou.com/login/login.html # 2銆佽姹傛柟娉:GET # 3銆佽姹傚ご: # User-agent r1 = requests.get('https://passport.lagou.com/login/login.html',
                 headers={ 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
                 },
                 )

X_Anti_Forge_Token = re.findall("X_Anti_Forge_Token = '(.*?)'", r1.text, re.S)[0]
X_Anti_Forge_Code = re.findall("X_Anti_Forge_Code = '(.*?)'", r1.text, re.S)[0] print(X_Anti_Forge_Token, X_Anti_Forge_Code) # print(r1.cookies.get_dict()) # 绗簩姝ワ細鐧婚檰 # 1銆佽姹倁rl:https://passport.lagou.com/login/login.json # 2銆佽姹傛柟娉:POST # 3銆佽姹傚ご: # cookie # User-agent # Referer:https://passport.lagou.com/login/login.html # X-Anit-Forge-Code:53165984 # X-Anit-Forge-Token:3b6a2f62-80f0-428b-8efb-ef72fc100d78 # X-Requested-With:XMLHttpRequest # 4銆佽姹備綋锛 # isValidate:true # username:15131252215 # password:ab18d270d7126ea65915c50288c22c0d # request_form_verifyCode:'' # submit:'' r2 = requests.post( 'https://passport.lagou.com/login/login.json',
    headers={ 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36', 'Referer': 'https://passport.lagou.com/login/login.html', 'X-Anit-Forge-Code': X_Anti_Forge_Code, 'X-Anit-Forge-Token': X_Anti_Forge_Token, 'X-Requested-With': 'XMLHttpRequest' },
    data={ "isValidate": True, 'username': '15131255089', 'password': 'ab18d270d7126ea65915c50288c22c0d', 'request_form_verifyCode': '', 'submit': '' },
    cookies=r1.cookies.get_dict()
) print(r2.text)


鍙傝冿細

http://cn.python-requests.org/zh_CN/latest/user/quickstart.html#id4

http://www.python-requests.org/en/master/

http://docs.python-requests.org/en/latest/user/quickstart/

https://www.cnblogs.com/tangdongchu/p/4229049.html#t0

http://www.cnblogs.com/wupeiqi/articles/6283017.html


浠ヤ笂灏辨槸Python缃戠粶鐖櫕鍩虹妯″潡 - requests妯″潡璇﹁В鐨勮缁嗗唴瀹癸紝鏇村淇℃伅璇峰叧娉∣D浜戝叾瀹冪浉鍏虫枃绔狅紒



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