尝试使用带有标题的urllib,但我继续收到此错误:TypeError:需要一个float。我使用的是python 3.4

时间:2017-03-29 13:42:43

标签: python urllib

我一直在尝试使用自定义标头发送请求,但python继续提供此错误消息,我出错了什么? 这是我的代码以及python的完整响应:

代码

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

1 个答案:

答案 0 :(得分: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)

你的第三个标题&#34;参数被传递到timeout

urlopen参数

您需要使用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)