Python 如何提交表格

Python 如何提交表格,python,python-3.x,python-requests,Python,Python 3.x,Python Requests,我想进入网站并发送我的数据(我的代码),然后我想返回 我尝试了下面的代码,但没有成功 import requests url = "http://duckduckgo.com/html" payload = {'q':'python'} r = requests.post(url, payload) with open("requests_results.html", "w") as f: f.write(r.content) 当我尝试它时,我得到的只是一个奇怪的HTML表单,但不是

我想进入网站并发送我的数据(我的代码),然后我想返回

我尝试了下面的代码,但没有成功

import requests

url = "http://duckduckgo.com/html"
payload = {'q':'python'}
r = requests.post(url, payload)
with open("requests_results.html", "w") as f:
    f.write(r.content)

当我尝试它时,我得到的只是一个奇怪的HTML表单,但不是我想要的数据。

这段代码做了一些工作,请自己尝试。请求的html文件使用相对寻址,因此如果不添加绝对寻址,它看起来很难看

import requests

code = '''
line 1
    line 2
        line 3
'''

payload = {
    'poster': 'Default Poster',
    'syntax': 'python',
    'content': code
    }

url = 'https://paste.ubuntu.com/'
r = requests.post(url, data=payload)
text = r.text

# If you want your html file to look the same as original
# then uncomment next line, otherwise it's ugly

# text = text.replace('href="/','href="https://paste.ubuntu.com/')

with open('requests_results.html', 'w') as f:
    f.write(text)