如何提交表格

时间:2017-06-25 11:37:31

标签: python python-3.x python-requests

我想进入网站https://paste.ubuntu.com/并发送我的数据(我的代码)然后我想回来。

我尝试了下面的代码,但它没有用。

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表单,但不是我想要的数据。

1 个答案:

答案 0 :(得分:0)

这段代码做了一些工作,自己试试。请求的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)