关于通过Python请求登录网站

2024-10-01 07:49:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我这里有个问题要解决,我被它困扰了两天。 我想用Python访问网站并获取未读消息的数量,但显然有一些小问题。我只是看到“客人”页面,而不是正确的登录页面。你知道吗

请帮我找出我的问题所在,我的代码如下。你知道吗

import requests
import os
import re
#import time

header_info ={
'Host': 'weavi.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'zh-TW,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding': 'gzip, deflate',
'Referer': 'https://weavi.com/',
'Connection': 'keep-alive',}


myS = requests.session()

r= myS.get("https://weavi.com")

myCookies= r.cookies

XSRF= re.findall('name="csrf-param" />\n<meta content=".+?" name="csrf-token" />',r.text,re.S)

XSRF_str= XSRF[0][36:-22]

login_data = {'username':'sera***@gmail.com','password':'***1230','redirect':'https://weavi.com/','authenticity_token':XSRF_str}

myS.post("https://weavi.com",data=login_data,headers=header_info,cookies=myCookies)

r=myS.get("https://weavi.com/")

message = re.findall('unread_message',r.text,re.S)

print message

Tags: texthttpsimportreinfocommessagedata
1条回答
网友
1楼 · 发布于 2024-10-01 07:49:04

网站中的表格张贴到/login

<form id="hidden-login-form" method="post" action="/login" style="display: none;">

所以试试看:

myS.post("https://weavi.com/login",data=login_data,headers=header_info,cookies=myCookies)

我没有一个有效的帐户进行测试,所以请让我知道如果有任何问题。你知道吗

相关问题 更多 >