Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

为什么urllib.error.URLError捕获不了异常 #2

Closed
dirtyluke opened this issue Dec 27, 2016 · 3 comments
Closed

为什么urllib.error.URLError捕获不了异常 #2

dirtyluke opened this issue Dec 27, 2016 · 3 comments

Comments

@dirtyluke
Copy link

我的代码在这里
用的是python3.6,抓取主播信息时老是报URLError错误,但是却捕获不了,能帮忙看一下吗

@octans
Copy link
Owner

octans commented Dec 28, 2016

@dirtyluke 贴一下你的报错信息,看能不能尽量详细一些

@dirtyluke
Copy link
Author

不好意思现在才回复。。。
74339197:html parse error in getUserData
Traceback (most recent call last):
File "D:\Program Files\Python\Python36\lib\urllib\request.py", line 1314, in do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "D:\Program Files\Python\Python36\lib\http\client.py", line 1155, in request
self._send_request(method, url, body, headers)
File "D:\Program Files\Python\Python36\lib\http\client.py", line 1200, in _send_request
self.endheaders(body)
File "D:\Program Files\Python\Python36\lib\http\client.py", line 1151, in endheaders
self._send_output(message_body)
File "D:\Program Files\Python\Python36\lib\http\client.py", line 983, in _send_output
self.send(msg)
File "D:\Program Files\Python\Python36\lib\http\client.py", line 926, in send
self.connect()
File "D:\Program Files\Python\Python36\lib\http\client.py", line 898, in connect
(self.host,self.port), self.timeout, self.source_address)
File "D:\Program Files\Python\Python36\lib\socket.py", line 711, in create_connection
raise err
File "D:\Program Files\Python\Python36\lib\socket.py", line 702, in create_connection
sock.connect(sa)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "huajiao.py", line 218, in
main(sys.argv)
File "huajiao.py", line 196, in main
spiderUserDatas()
File "huajiao.py", line 130, in spiderUserDatas
userId = getUserId(liveId)
File "huajiao.py", line 29, in getUserId
html = urlopen("http://www.huajiao.com/l/" + str(liveId),timeout=1)
File "D:\Program Files\Python\Python36\lib\urllib\request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "D:\Program Files\Python\Python36\lib\urllib\request.py", line 526, in open
response = self._open(req, data)
File "D:\Program Files\Python\Python36\lib\urllib\request.py", line 544, in _open
'_open', req)
File "D:\Program Files\Python\Python36\lib\urllib\request.py", line 504, in _call_chain
result = func(*args)
File "D:\Program Files\Python\Python36\lib\urllib\request.py", line 1342, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "D:\Program Files\Python\Python36\lib\urllib\request.py", line 1316, in do_open
raise URLError(err)
urllib.error.URLError:

@dirtyluke
Copy link
Author

解决了,自己前面设置的超时设置太长了,网上说的是可能服务器自动断开了连接,但客户端不知道,才产生的错误,把超时时间缩短就能捕获了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants