Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python3中提交web表单_Python_Python 2.7_Python 3.x - Fatal编程技术网

在Python3中提交web表单

在Python3中提交web表单,python,python-2.7,python-3.x,Python,Python 2.7,Python 3.x,请某人将以下内容从python2转换为python3 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) 我得到了 Traceback (most recent call last): File "C:

请某人将以下内容从python2转换为python3

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)
我得到了

Traceback (most recent call last):
File "C:\temp\Python\testFile.py", line 1, in <module>
import requests
ImportError: No module named 'requests'
但我明白了

Traceback (most recent call last):
File "C:\temp\Python\testFile.py", line 5, in <module>
r = urllib.request.post(url, payload)
AttributeError: 'module' object has no attribute 'post'
回溯(最近一次呼叫最后一次):
文件“C:\temp\Python\testFile.py”,第5行,在
r=urllib.request.post(url,有效负载)
AttributeError:“模块”对象没有属性“post”

因此,在python3.2中,r.content是一个bytestring,而不是str,write不喜欢它。您可能希望改用r.text:

with open("requests_results.html", "w") as f:
    f.write(r.text)
您可以在中的请求文档中看到它

编码将被猜测

编辑:


我在看到编辑后的问题之前发布了帖子。是的,正如Martijn Pieters所说,您需要为python3安装请求模块才能导入它。

我认为这里的问题是没有安装请求包。或者,如果您已经安装了它,它将安装在您的python2.x目录中,而不是python3中,这就是为什么您不能使用requests模块的原因。尝试将python3作为默认副本,然后安装请求


另外,请尝试访问Michael Foord的这篇文章,它将指导您使用urlib2的所有功能,当您在Python 3上这样做时,您遇到了什么问题?这是回溯(最近一次调用):文件“C:\temp\Python\testFile.py”,第1行,在import requests ImportError中:没有为Python 3安装名为'requests'的模块吗?它不是一个标准库(在Python 2或3中)。我读到请求已被urllib.request替换,对吗?@scott no.
urllib.request
是以前(py2)的
urllib
urllib2
。是一个外部库。这绝对不是问题所在。是的,原始问题没有指定实际错误。在python3中提交web表单的最简单方法是什么?当然,不要忘记更多信息。虽然这段代码可以回答这个问题,但提供有关如何以及为什么解决问题的信息可以提高其长期价值。
import urllib.request
import urllib.parse

url = "https://duckduckgo.com/html"
values = {'q':'python'}
data = urllib.parse.urlencode(values).encode("utf-8")

req = urllib.request.Request(url, data)
response = urllib.request.urlopen(req)
the_page = response.read()

print(the_page)
class requests.Response
    content - Content of the response, in bytes.
    text - Content of the response, in unicode. if Response.encoding is None and chardet     module is available,
import urllib.request
import urllib.parse

url = "https://duckduckgo.com/html"
values = {'q':'python'}
data = urllib.parse.urlencode(values).encode("utf-8")

req = urllib.request.Request(url, data)
response = urllib.request.urlopen(req)
the_page = response.read()

print(the_page)