Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python进行Web登录_Python_Python 3.x_Web Scraping_Scripting_Python Requests - Fatal编程技术网

使用python进行Web登录

使用python进行Web登录,python,python-3.x,web-scraping,scripting,python-requests,Python,Python 3.x,Web Scraping,Scripting,Python Requests,我想登录到给定的URL。我尝试了一个解决方案,但无法验证它是否有效。如何确保它正确登录。任何建议都会有帮助。谢谢 我是初学者 import requests #from lxml import html login_url = 'https://epaper.thehindu.com' req_url = 'https://epaper.thehindu.com/Home/Index' payload = { "Email": "email", "Password":"pas

我想登录到给定的URL。我尝试了一个解决方案,但无法验证它是否有效。如何确保它正确登录。任何建议都会有帮助。谢谢 我是初学者

 import requests

#from lxml import html
login_url = 'https://epaper.thehindu.com'
req_url = 'https://epaper.thehindu.com/Home/Index'
payload = {
    "Email": "email",
    "Password":"pass"

}

with requests.Session() as session:
    post = session.post(login_url, data=payload)
    r = session.get(req_url)
    print (r.text)

您可以尝试检查请求会话cookie是否已更改。登录通常会将cookie添加到会话cookie jar中,您可以通过编程方式进行检查

import requests

target_url = 'url'
target_user = 'user'
target_pw = 'password'

payload = {
    'username': target_user,
    'password': target_pw
}

with requests.Session() as session:
    print(session.cookies)
    r = session.post(target_url, data=payload)
    print(session.cookies)
    session.close()

试试下面的方法。我想你现在得到了一个有效的答复。对于有效负载参数仍然存在一些混淆,因为我无法以正确的方式检查它,因为我无法访问该页面。确保在有效负载字段中输入
电子邮件
密码
。试一试:

import requests
from bs4 import BeautifulSoup

url = 'https://epaper.thehindu.com/'
req_url = 'https://epaper.thehindu.com/Login/ValidateLogin'

with requests.Session() as session:
    res = session.get(url)
    soup = BeautifulSoup(res.text,"lxml")

    payload={
        '__RequestVerificationToken':soup.select_one("[name='__RequestVerificationToken']")['value'],
        'Email':'',  #your email
        'Password':'', #your password
        'hiddenTab':'' 
        }
    r = session.post(req_url,data=payload)
    soup = BeautifulSoup(r.text,"lxml")
    print(soup.title)

结果:正在打印页面的标题。。。隐藏选项卡怎么样?为什么使用requestverificationtokenTry使用chrome开发工具查看post请求是如何处理的,以及哪些参数需要随请求一起传递。如果在chrome开发工具的
网络
部分中按下
xhr
选项卡下填充的右链接,可以找到我在
有效负载
中使用的字段。顺便说一句,如果它有效,请确保通过勾选我的答案旁边的
复选标记来接受它作为解决方案。