https://2.python-requests.org//zh_CN/latest/user/quickstart.html
1 #用戶名密碼登錄 2 1、尋找登錄請求(此處能夠故意輸錯用戶名密碼,目的是爲了可以看清楚重定向的地址) 3 發現: 4 點擊登錄時,請求了 5 ①、post302:https://passport.mafengwo.cn/login 6 ②、get200:https://passport.mafengwo.cn/ 7 8 兩個請求(request)攜帶的cookies都是同樣的 9 但返回(response)的cookies爲空-----由於登錄不成功 10 提取到的信息: 11 request header: 12 :authority:passport.mafengwo.cn 13 :method:POST 14 :path:/login 15 :scheme:https 16 accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 17 accept-encoding:gzip, deflate 18 accept-language:zh-CN,zh;q=0.8 19 cache-control:max-age=0 20 content-length:28 21 content-type:application/x-www-form-urlencoded 22 cookie:mfw_uuid=5c64cb93-1d86-90ad-221e-1326749de333; oad_n=a%3A3%3A%7Bs%3A3%3A%22oid%22%3Bi%3A1029%3Bs%3A2%3A%22dm%22%3Bs%3A15%3A%22www.mafengwo.cn%22%3Bs%3A2%3A%22ft%22%3Bs%3A19%3A%222019-02-14+09%3A59%3A47%22%3B%7D; __mfwlv=1550109540; __mfwvn=1; uva=s%3A78%3A%22a%3A3%3A%7Bs%3A2%3A%22lt%22%3Bi%3A1550109587%3Bs%3A10%3A%22last_refer%22%3Bs%3A6%3A%22direct%22%3Bs%3A5%3A%22rhost%22%3Bs%3A0%3A%22%22%3B%7D%22%3B; __mfwurd=a%3A3%3A%7Bs%3A6%3A%22f_time%22%3Bi%3A1550109587%3Bs%3A9%3A%22f_rdomain%22%3Bs%3A0%3A%22%22%3Bs%3A6%3A%22f_host%22%3Bs%3A3%3A%22www%22%3B%7D; __mfwuuid=5c64cb93-1d86-90ad-221e-1326749de333; UM_distinctid=168e9ba87c647c-08ff021bcc1d08-4d015463-13c680-168e9ba87c728d; PHPSESSID=9shbteed3ii63dsn2porlmu9u6; mafengwo=77822214db865f501b408c46b6875961_86305484_5c64cd836e4e33.63070874_5c64cd836e4ed4.41668449; mfw_uid=86305484; uol_throttle=86305484; __mfwlt=1550110249 23 "origin":https://passport.mafengwo.cn 24 'referer':https://passport.mafengwo.cn/ 25 'upgrade-insecure-requests':1 26 'user-agent': 27 28 From Data: 29 'passport':'136160XXXX'; 30 'password':'SDA';(錯誤的密碼) 31 32 若輸入正確的用戶名密碼,再查看返回的(response)的cookies是有值的,說明登錄成功 33 >>>>代碼以下,可運行 34 #by zhonghui 35 # -*- coding: utf-8 -*- 36 import requests; 37 import sys; 38 import io; 39 from bs4 import BeautifulSoup; 40 import ssl; 41 ssl._create_default_https_context = ssl._create_unverified_context; 42 43 Useragents='Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36'; 44 header={ 45 "origin":"https://passport.mafengwo.cn", 46 "referer":"https://passport.mafengwo.cn/", 47 "upgrade-insecure-requests":"https://passport.mafengwo.cn/", 48 'user-agent':Useragents 49 50 }; 51 52 def login(): 53 postUrl='https://passport.mafengwo.cn/'; 54 username=input('請輸入帳號'); 55 PSW=input('請輸入密碼:'); 56 57 postData={ 58 'passport':username, 59 'password':PSW 60 61 } 62 63 res=requests.post(postUrl,data=postData,headers=header,verify=False); 64 65 print('執行成功'); 66 pass; 67 68 if __name__=='__main__': 69 login();
#遇到的問題:
請求https時,出現須要證書,提示443錯誤,根據 1 ①、關掉電腦上的代理(fillder)---無效
2 ②、嘗試導入證書相關模塊----無效 3 pip install cryptography 4 pip install pyOpenSSL 5 pip install certifi 6 ③、res=requests.post(postUrl,data=postData,headers=header,verify=False);----無效 7 ④、 代碼頁加入如下這個------無效 8 #from requests.packages.urllib3.exceptions import InsecureRequestWarning; 9 10 # 禁用安全請求警告 11 #requests.packages.urllib3.disable_warnings(InsecureRequestWarning); 12 13 ⑤、代碼頁加入如下-----最終運行成功 14 import ssl; 15 ssl._create_default_https_context = ssl._create_unverified_context; 16 17 18 19 最終結果: 20 雖運行成功,但仍提示一個錯誤,目前未解決,百度了一下,意思是【強烈建議添加證書驗證】 21 28 這個提示實際並不影響正確的結果,只不過提示若是一直存在也不太美觀,因此能夠加入下面兩行代碼。取消警告 29 from requests.packages.urllib3.exceptions import InsecureRequestWarning 30 # 禁用安全請求警告 31 requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
-===================總結=============================html
想要訪問httpspython
①、請求時,加上【verify=False】web
②、引入如下代碼安全
import ssl;
ssl._create_default_https_context = ssl._create_unverified_context;