python实操登录指定网站
分类专栏: Python
简介 python实操登录指定网站,供参考学习, 样式给广大开发者参考。
<pre class="ql-syntax" spellcheck="false">import requests
import re
import time
index_url = "http://account.chinaunix.net/login"
header = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Cookie": "Hm_lvt_0ee5e8cdc4d43389b3d1bfd76e83216b=1610677524; Hm_lpvt_0ee5e8cdc4d43389b3d1bfd76e83216b=1610677524; __pta=246804210.1610677525.1610677525.1610677525.1; __pts=582931432; __ptb=582931432; account_chinauni=accountchinauni; XSRF-TOKEN=hDgiUtrbWhkrDhdKhtsOgwRYomqqyUE6c9gCcyQA; laravel_session=APKvoIT7gV1FoUQ78zmqSCglMjjQbucPqcEbA7Sa; reg_referer=account.chinaunix.net; captcha_gee=6000fd4b74f20; st_user_token=849b8de97ba812e2a844d51cdb57e194",
"Host": "account.chinaunix.net",
"Pragma": "no-cache",
"Referer": "http://account.chinaunix.net/login/out",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
}
headers = {
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Accept-Encoding":"gzip, deflate",
"Accept-Language":"zh-CN,zh;q=0.9",
"Connection":"keep-alive",
"Host":"account.chinaunix.net",
"Upgrade-Insecure-Requests":"1",
"User-Agent":"Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3610.2 Safari/537.36",
}
login_session = requests.session()
# 正则表达式,
token_search = re.compile(r'name="_token"\svalue="(.*?)"')
# 一定要使用login_session,否则无法保存cookie
index_response = login_session.get(url=index_url, headers=headers)
# print(index_response.text)
# 匹配文本中的token
token_value = re.search(token_search, index_response.text).group(1)
print(token_value)
# 开始构造登录数据
data = {
"username": "ab135790",
"password": "bomei123",
"_token": token_value,
"_t": int(time.time()) # 经过分析是时间戳
}
# 登录的时候使用的接口,使用的URL
login_url = 'http://account.chinaunix.net/login/login'
# 使用的是POST请求
login_response = login_session.post(url=login_url, headers=headers, data=data)
print(login_response.text)
# 再去请求设置页面的
phone_url = 'http://account.chinaunix.net/ucenter/user/index'
phone_response = login_session.get(url=phone_url, headers=headers)
# 是可以看到设置页面的手机号
print(phone_response.text)
</pre><p><br></p>
分享到:
转载:
喜欢 0
收藏
暂无评论信息
- 相关文章
- 文章推荐
-
淘宝 NPM 镜像站喊你切换新域名啦
淘宝 NPM 镜像站(npm.taobao.org)自 2014 年 正式对外服务,一开始只是想简单地做 NPM 的中国镜像站点,回馈国内前端社区,不知不觉竟然一直运行到现在。当年参考 Ruby Gems 淘宝镜像 的方式,跟阿里开源组织申请了 taobao.org 的二级域名,镜像站点名称也自然而然地取名为 淘宝 NPM 镜像站 (下称 CNPM)。
-
乐视官方确认还有 400 多人:消息称员工“没有内卷和 996”,过着“没有老板”的神仙日子
7 月 11 日消息,近期,微博博主 @Fenng 分享了一个冷知识:乐视居然还剩下 400 多人,有不少是五年以上的老员工。这些老员工称“幸福感是很多老乐视人留下来的理由”,没有内卷和 996,没拖欠过留下来的员工的工资,也没停过社保。
-
Docker 常用命令
docker目前代替虚拟机使用的一个容器,灵活好用。
-
docker搭建jenkins环境执行宿主机的docker无权限的解决方法
初次搭建jenkins持续集成工具的时候,在运行项目阶段出现 permission denied的情况
- 点击排行
- 站长推荐
- 猜你喜欢
- 网站信息
- 站内问答:12篇
- 站内文章:213篇
- 建站时间:已运行1267天
- 备案号: 浙ICP备2022018799号
- 语言:
- English(USA)
- French(FR)
- Chinese(ZH)
无数据