我有一个小的搜寻器,并且正在提取一个简单页面的网页内容。
def url2dict(url):
'''
DOCSTRING: converts two-column data into a dictionary with first column as a key.
INPUT: URL address as a string
OUTPUT: dictionary with one key and one value
'''
with urlopen(url) as page:
page_raw = page.read()
...
现在此函数在url处调用服务器。问题是服务器已生成504错误
File "C:\Python38\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "C:\Python38\lib\urllib\request.py", line 531, in open
response = meth(req, response)
File "C:\Python38\lib\urllib\request.py", line 640, in http_response
response = self.parent.error(
File "C:\Python38\lib\urllib\request.py", line 569, in error
return self._call_chain(*args)
File "C:\Python38\lib\urllib\request.py", line 502, in _call_chain
result = func(*args)
File "C:\Python38\lib\urllib\request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 504: Gateway Time-out
我的问题是我找不到urlopen超时的默认值。
这里https://bugs.python.org/issue18417表示默认情况下没有超时(超时=无)(至少对于Python 3.4版本而言::]]
[确定,我复查了此问题足以记住:如果从不调用socket.setdefaulttimeout,则默认超时为None(无超时)。
3.8的当前状态是什么?
如果未设置超时,为什么会收到错误504的错误?
更多详细信息:
其中一个错误显示]中的错误>
File "C:\Python38\lib\urllib\request.py", line 222, in urlopen return opener.open(url, data, timeout)
我打开文件并且已经阅读:
def urlopen(URL,data = None,timeout = socket._GLOBAL_DEFAULT_TIMEOUT,*,cafile = None,capath = None,cadefault = False,context = None):'''打开URL URL,它可以是字符串或Request对象。
的全局变量,它将用作超时持续时间吗?*data* must be an object specifying additional data to be sent to the server, or None if no such data is needed. See Request for details. urllib.request module uses HTTP/1.1 and includes a "Connection:close" header in its HTTP requests. The optional *timeout* parameter specifies a timeout in seconds for blocking operations like the connection attempt (if not specified, the global default timeout setting will be used). This only works for HTTP, HTTPS and FTP connections.
所以(如果未指定,将使用全局默认超时设置)表示如果我定义了一个名为timeout
我的爬虫很小,我正在提取一个简单页面的网页内容。 def url2dict(url):'''DOCSTRING:将两列数据转换为以第一列为键的字典。 ...
您的研究实际上是正确的,默认超时由socket._GLOBAL_DEFAULT_TIMEOUT
确定。要了解其值,可以使用socket.getdefaulttimeout()
:
返回新套接字对象的默认超时时间,以秒为单位(
socket.getdefaulttimeout()
)。值float
表示新的套接字对象没有超时。首次导入套接字模块时,默认值为None
。