2017-03-29 10 views
0

我一直在尝试使用自定义标头发送请求,但是python不停地给出这个错误消息,我会发生什么错误? 这里是我的代码与蟒蛇充分反应起来:试图使用urllib与标题,但我一直得到这个错误:TypeError:一个浮动是必需的。我正在使用python 3.4

代码

import urllib.request 
import urllib.parse 

url = 'https://nytimes.com/' 

user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' 
values = {'name': 'Kromanion Krank', 'location': 'Finland', 'language': 'Python'} 
headers = {'User-Agent': user_agent} 
data = urllib.parse.urlencode(values) 
data = data.encode('ascii') 
html_str = urllib.request.urlopen(url, data, headers) 
html_txt = html_str.text 
print(html_txt) 

输出错误

Traceback (most recent call last): 
    File "C:/Users/fpt84/PycharmProjects/WebC/TEST.py", line 11, in <module> 
    html_str = urllib.request.urlopen(url, data, headers) 
    File "C:\Python34\lib\urllib\request.py", line 161, in urlopen 
    return opener.open(url, data, timeout) 
    File "C:\Python34\lib\urllib\request.py", line 464, in open 
    response = self._open(req, data) 
    File "C:\Python34\lib\urllib\request.py", line 482, in _open 
    '_open', req) 
    File "C:\Python34\lib\urllib\request.py", line 442, in _call_chain 
    result = func(*args) 
    File "C:\Python34\lib\urllib\request.py", line 1226, in https_open 
    context=self._context, check_hostname=self._check_hostname) 
    File "C:\Python34\lib\urllib\request.py", line 1183, in do_open 
    h.request(req.get_method(), req.selector, req.data, headers) 
    File "C:\Python34\lib\http\client.py", line 1137, in request 
    self._send_request(method, url, body, headers) 
    File "C:\Python34\lib\http\client.py", line 1182, in _send_request 
    self.endheaders(body) 
    File "C:\Python34\lib\http\client.py", line 1133, in endheaders 
    self._send_output(message_body) 
    File "C:\Python34\lib\http\client.py", line 963, in _send_output 
    self.send(msg) 
    File "C:\Python34\lib\http\client.py", line 898, in send 
    self.connect() 
    File "C:\Python34\lib\http\client.py", line 1279, in connect 
    super().connect() 
    File "C:\Python34\lib\http\client.py", line 871, in connect 
    self.timeout, self.source_address) 
    File "C:\Python34\lib\socket.py", line 504, in create_connection 
    sock.settimeout(timeout) 
TypeError: a float is required 

回答

0

urllib.request.urlopen(从https://docs.python.org/3/library/urllib.request.html#urllib.request.urlopen)签名是:

urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None) 

你的第三个“头文件”参数是gett ING传入的urlopen

timeout说法你需要使用一个Request对象做你想要什么:

req = urllib.request.Request(url, data, headers) 
resp = urllib.request.urlopen(req) 

从你的代码示例(我也不得不修改响应的使用)

import urllib.request 
import urllib.parse 

url = 'https://nytimes.com/' 

user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' 
values = {'name': 'Kromanion Krank', 'location': 'Finland', 'language': 'Python'} 
headers = {'User-Agent': user_agent} 
data = urllib.parse.urlencode(values) 
data = data.encode('ascii') 
request = urllib.request.Request(url, data, headers) 
resp = urllib.request.urlopen(request) 
html_txt = resp.read().decode('UTF-8') 
print(html_txt) 
+0

感谢您的答复,这帮助了,但是现在,当我尝试打印** ** html_txt我得到这个错误:** UnicodeEncodeError:“字符映射”编解码器不能在21812-21813位置编码字符:字符地图 * *我可以使用哪种解码方法来打印html_txt? – Pozeidon

+0

嘿家伙!刚刚发生的事情,我刚刚更新到Pycharm 2016.x,我的代码现在运行没有问题。我的意思是代码错误** UnicodeEncodeError:'charmap'编解码器无法对位置21812-21813中的字符进行编码:字符映射为 **不再显示。有人能解释为什么吗?因为我不明白。 – Pozeidon

+0

Windows的默认终端编码是cp1252(aka charmap)。并非所有的UTF-8字符都可以在cp1252中编码(事实上,cp1252中的_most_ utf-8字符是不可编码的)。 –

相关问题