![](/img/trans.png)
[英]Using CGI in python 3.4 I keep getting “End of script output before headers: measurement.py” error in my error log
[英]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)
您的第三個“標頭”參數將傳遞給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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.