python请求(表单填写)

时间:2017-04-16 14:33:56

标签: python-3.x

这是指向页面的链接 填写表格......

https://anotepad.com/notes/2yrwpi

我必须在文本区域中输入内容(比如说)(“hello world”),然后按save,但所有这些都是用python request模块完成的(获取,发布等) 并且不使用硒和beautifulsoup模块。

我尝试过类似的事情:

url="https://anotepad.com/notes/2yrwpi"
txt = "Hello World"

#construct the POST request
form_data = {'btnSaveNote':'Save', 'notecontent' : txt} 

post = requests.post(url,data=form_data)

但这似乎不起作用

请帮忙!

1 个答案:

答案 0 :(得分:1)

您需要登录并发布到保存网址,您还需要在表单数据中传递注释编号:

import requests

save = "https://anotepad.com/note/save"
txt = "Hello World"
login = "https://anotepad.com/create_account"

data = {"action": "login",
        "email": "you@whatever.com",
        "password": "xxxxxx",
        "submit": ""}

# construct the POST request
with requests.session() as s: # Use a Session object.
    s.post(login, data) # Login.

    form_data = {"number": "2yrwpi",
                 "notetype": "PlainText",
                 "noteaccess": "2",
                 "notequickedit": "false",
                 "notetitle": "whatever",
                 "notecontent": txt}

    r = s.post(save, data=form_data) # Save note.

r.json()会在成功时为您{"message":"Saved"}提供帮助。此外,如果您想查看自己的注释,请在登录后运行s.post("https://anotepad.com/note/list").text