python模拟登陆实战(1)

来源:互联网 发布:手机模拟器连发软件 编辑:程序博客网 时间:2024/06/14 05:15

今天看到了这个博客:点击打开链接,也尝试着如博客中一样,登陆V2EX。

由于网站版本迭代了,需要做修改。

先进入

https://www.v2ex.com/signin
在chrome中按F12打开开发者工具,然后登陆该网站,此时在Network中左边的Name中找到signin,然后在Headers中找到From Data,如下:


我们需要post的数据是Form Data中的那一长串,而非是原博主的那样,key是固定的叫做User和pwd,所以这两个长串也是我们需要匹配出来的,这两个长串是在登陆页面中的html中,以及once的值。

这里通过正则表达式匹配出来,然后post就ok了。

代码如下:

import reimport requestsUA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' \     'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36'url = 'https://www.v2ex.com/signin'header = {    'User-Agent': UA,    'Referer': url}s = requests.session()content = s.get(url=url, headers=header).content.decode('utf-8')# print(content)p1 = r'<input type="text" class="sl" name="(.*?)"'p2 = r'<input type="password" class="sl" name="(.*?)"'p3 = r"location.href = '/auth/google\?once=([0-9]+)';"user = re.findall(pattern=p1, string=content, flags=re.S)[0]pwd = re.findall(pattern=p2, string=content, flags=re.S)[0]once = re.findall(pattern=p3, string=content, flags=re.S)[0]print(user)print(pwd)print(once)data = {    user: '填你的账户',    pwd:  '填你的密码',    'once': once,    'next': '/'}# print(data[user])# print(data[pwd])# print(data['once'])s.post(url=url, data=data, headers=header)url = 'http://www.v2ex.com/settings'content = s.get(url=url, headers=header).content.decode('utf-8')print(content)

0 0
原创粉丝点击