如何将数据发送到表单并使用python请求发送表单?

时间:2018-03-25 09:31:45

标签: python beautifulsoup python-requests

我想转到链接,然后将数据发送到表单然后表单本身,通过请求只通过链接中需要指定的参数处理,但我无法获得完整的发送表单的正确链接,即我知道链接,以及在发送我不知道的数据后如何形成链接。

如果您打开开发工具,并转到网络面板,参数中会显示这样的行:

Query string
    action Login
    module Users

Form data
    __vtrftk sid: 34b6b3446d78b01ccb1e5678c89c86365a280a51,1521970011
    password you
    username hey

如果你去Headers并点击Edit and Resend按钮:那里会有一个"Request Body",那里有三个字段,csrf,user和pass。

__vtrftk=sid%3A34b6b3446d78b01ccb1e5678c89c86365a280a51%2C1521970011&username=hey&password=you

如何使用请求登录python脚本?

这是请求http://74.207.247.199:8181/index.php?module=Users&action=Login

的链接

这是我糟糕的剧本

import sys
import requests
from requests import Session
from bs4 import BeautifulSoup


target = 'http://74.207.247.199:8181/'

sec_target = 'http://74.207.247.199:8181/index.php?module=Users&action=Login'
def main(target, sec_target):
    with requests.Session() as session:
        # start of auth
        s = Session()
        get_csrf_token = s.get(target).text
        soup = BeautifulSoup(get_csrf_token, 'lxml')
        find_scrf_token_body = soup.find('div', id='loginDiv')
        find_csrf_token_input = find_scrf_token_body.find('input', {"name":"__vtrftk"}).attrs['value']
        #payload ={'__vtrftk':find_csrf_token_input+'&username=someuser&password=somepass'}
        headers = {
        '__vtrftk': find_csrf_token_input,
        'username': 'someuser',
        'password': 'somepass',
        'User-Agent':"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0",
        'Content-Type': 'application/x-www-form-urlencoded',
        'Connection': 'keep-alive',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Referer': 'http://74.207.247.199:8181/',
        }
        # Отправляем данные в POST, в session записываются наши куки
        f = s.post(sec_target, headers=headers)
        g = s.get('http://74.207.247.199:8181/index.php?module=Users&action=Login').text
        soup = BeautifulSoup(g, 'lxml')
        find_alert_body = soup.find('div', id='loginDiv')
        find_alert = find_alert_body.find('div', class_='alert alert-error')
        print(find_alert)
        print(f.url)




if __name__ == '__main__':
    main(target, sec_target)

0 个答案:

没有答案