繁体   English   中英

尝试将urllib与标头一起使用,但我不断收到此错误:TypeError:需要浮点数。 我正在使用python 3.4

[英]Trying to use urllib with headers but I keep on getting this error: TypeError: a float is required. I'm using python 3.4

我一直在尝试使用自定义标头发送请求,但是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

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)

您的第三个“标头”参数将传递给urlopentimeout参数

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM