python--模拟蜂窝网(https)登陆总结

时间:2023-03-09 03:42:33
python--模拟蜂窝网(https)登陆总结
https://2.python-requests.org//zh_CN/latest/user/quickstart.html 
1 #用户名密码登陆
1、寻找登陆请求(此处可以故意输错用户名密码,目的是为了能够看清楚重定向的地址)
发现:
点击登陆时,请求了
①、post302:https://passport.mafengwo.cn/login
②、get200:https://passport.mafengwo.cn/ 两个请求(request)携带的cookies都是一样的
但返回(response)的cookies为空-----因为登陆不成功
提取到的信息:
request header:
:authority:passport.mafengwo.cn
:method:POST
:path:/login
:scheme:https
accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
accept-encoding:gzip, deflate
accept-language:zh-CN,zh;q=0.8
cache-control:max-age=0
content-length:28
content-type:application/x-www-form-urlencoded
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
"origin":https://passport.mafengwo.cn
'referer':https://passport.mafengwo.cn/
'upgrade-insecure-requests':1
'user-agent': From Data:
'passport':'136160XXXX';
'password':'SDA';(错误的密码) 若输入正确的用户名密码,再查看返回的(response)的cookies是有值的,说明登陆成功
>>>>代码如下,可运行
#by zhonghui
# -*- coding: utf-8 -*-
import requests;
import sys;
import io;
from bs4 import BeautifulSoup;
import ssl;
ssl._create_default_https_context = ssl._create_unverified_context; 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';
header={
"origin":"https://passport.mafengwo.cn",
"referer":"https://passport.mafengwo.cn/",
"upgrade-insecure-requests":"https://passport.mafengwo.cn/",
'user-agent':Useragents }; def login():
postUrl='https://passport.mafengwo.cn/';
username=input('请输入账号');
PSW=input('请输入密码:'); postData={
'passport':username,
'password':PSW } res=requests.post(postUrl,data=postData,headers=header,verify=False); print('执行成功');
pass; if __name__=='__main__':
login();

#遇到的问题:
请求https时,出现需要证书,提示443错误,根据 ①、关掉电脑上的代理(fillder)---无效
 ②、尝试导入证书相关模块----无效
pip install cryptography
pip install pyOpenSSL
pip install certifi
③、res=requests.post(postUrl,data=postData,headers=header,verify=False);----无效
④、 代码页加入以下这个------无效
#from requests.packages.urllib3.exceptions import InsecureRequestWarning; # 禁用安全请求警告
#requests.packages.urllib3.disable_warnings(InsecureRequestWarning); ⑤、代码页加入以下-----最终运行成功
14 import ssl;
15 ssl._create_default_https_context = ssl._create_unverified_context; 最终结果:
虽运行成功,但仍提示一个错误,目前未解决,百度了一下,意思是【强烈建议添加证书验证】 这个提示实际并不影响正确的结果,只不过提示如果一直存在也不太美观,所以可以加入下面两行代码。取消警告
from requests.packages.urllib3.exceptions import InsecureRequestWarning
# 禁用安全请求警告
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)


python--模拟蜂窝网(https)登陆总结

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

想要访问https

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

②、引入以下代码

import ssl;
ssl._create_default_https_context = ssl._create_unverified_context;