当“按钮”输入字段没有名称时,如何使用Python请求登录

问题描述 投票:0回答:1

我正在尝试使用请求登录网站。在我的post请求中,我必须发送表单的所有输入字段。似乎按钮输入的name字段不存在,所以我不知道在该字段中放置哪个值/输入字段如下所示:

<input type="text" name="email_address" size="40" class="input-text">
<input type="password" name="password" size="40" class="input-text">
<input type="submit" class="button-normal" value="Log In">

登录页面的for是:

<form name="login" action="https://www.southernhobby.com/login.php?action=process" method="post"></form>

我使用以下代码尝试登录:

payload = {'email_address': 'email', 'password': 'password', '':'Log In'}
login_url = 'https://www.southernhobby.com/login.php?action=process'
url = 'https://www.southernhobby.com/ccg-s/magic-the-gathering/c13_362/'

with requests.session() as s:
    s.get(login_url)
    s.post(login_url, data=payload)
    response = s.get(url)
    print(response.text)

我打印的响应URL是一个页面,其中包含只有用户才能看到的信息。当然代码失败了,我相信这是因为我没有提交按钮的输入值。我能放在这里什么?谢谢。

web-scraping python-requests
1个回答
0
投票

您的脚本无法在网站中登录的主要原因是由于没有标题。实现它总是一个好主意。总的来说你的代码应该是这样的

import requests

payload = {'email_address': 'email', 'password': 'password'}

headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:61.0) Gecko/20100101 Firefox/61.0",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Language": "en-US,en;q=0.5",
    "Accept-Encoding": "gzip, deflate",
    "Referer": "https://www.southernhobby.com/login.php",
    "Content-Type": "application/x-www-form-urlencoded",
    "Content-Length": "34",
    "DNT": "1",
    "Connection": "close",
    "Upgrade-Insecure-Requests": "1",
}

with requests.session() as s:
    s.post('https://www.southernhobby.com/login.php?action=process', data=payload, headers = headers)
    response = s.get('https://www.southernhobby.com/ccg-s/magic-the-gathering/c13_362/', headers=headers)
    print(response.text)

正如你所看到的,我也删除了s.get(login_url)'':'Log In',因为这两个片段都没有为你的代码做出任何贡献。

希望这可以帮助你 :)

© www.soinside.com 2019 - 2024. All rights reserved.