zoukankan      html  css  js  c++  java
  • python--模拟蜂窝网(https)登陆总结

    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)


     -===================总结=============================

     想要访问https

    ①、请求时,加上【verify=False】

    ②、引入以下代码

    import ssl;
    ssl._create_default_https_context = ssl._create_unverified_context;
    
    
  • 相关阅读:
    python多线程学习一
    https学习笔记三----OpenSSL生成root CA及签发证书
    https学习笔记二----基础密码学知识和python pycrypto库的介绍使用
    HTTPS学习笔记一----HTTPS的基础理论知识
    一只小鹅的2017
    python 全局变量的import机制
    一起来学设计模式-----工厂模式的实践
    一起来学设计模式-----创建型模式之抽象工厂
    一起来学设计模式-----创建型模式之工厂方法
    一起来学设计模式-----创建型模式之简单工厂
  • 原文地址:https://www.cnblogs.com/shenyexiaoqingxin/p/10373404.html
Copyright © 2011-2022 走看看