python 爬取网页是出现的错误

zhongkouwei 2009-11-09 12:13:18
代码:
timeout = 10
socket.setdefaulttimeout(timeout)
sock = urllib2.urlopen(url)
html = sock.read()
一般情况能行,运行没问题,可有常常会突然报错
Traceback (most recent call last):
File "/usr/local/lib/python2.5/threading.py", line 486, in __bootstrap_inner
self.run()
File "spider_eastmoney.py", line 150, in run
next_page = parse_ (mysqlDB, http, html, mod_name)
File "spider_eastmoney.py", line 94, in parse_
page_html= http.get_html(pageurl)
File "/home/Spider/News/genericHttp.py", line 47, in get_html
File "/home/Spider/News/genericHttp.py", line 47, in get_html
html = sock.read()
File "/usr/local/lib/python2.5/socket.py", line 304, in read
data = self._sock.recv(rbufsize)
File "/usr/local/lib/python2.5/httplib.py", line 529, in read
s = self.fp.read(amt)
File "/usr/local/lib/python2.5/socket.py", line 328, in read
data = self._sock.recv(left)
timeout: timed out
这是什么问题,以前没遇过,我用了多线程,线程数我设为1,会不会是这个原因。
...全文
306 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zouhailin 2009-11-22
  • 打赏
  • 举报
回复
高人, 学到一招.
mrshelly 2009-11-21
  • 打赏
  • 举报
回复
while True:
try:

except:
time.sleep(2)
continue

if 数据正常 :
break

lsesunny 2009-11-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ddgg 的回复:]
用try...except把html = sock.read()包起来,在except里对异常进行捕捉,该忽略忽略,该重试重试。
[/Quote]
test
DDGG 2009-11-20
  • 打赏
  • 举报
回复
用try...except把html = sock.read()包起来,在except里对异常进行捕捉,该忽略忽略,该重试重试。
wangzheng7912 2009-11-16
  • 打赏
  • 举报
回复
这种情况需要你对它的Exception做个处理,具体可以参考python的网络编程
mrshelly 2009-11-15
  • 打赏
  • 举报
回复
网络情况造成的...

很正常....
Semigod 2009-11-09
  • 打赏
  • 举报
回复
这不是很明显地写着receive timeout吗
zhongkouwei 2009-11-09
  • 打赏
  • 举报
回复
我说,是什么原因造成的

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧