鱼C论坛

 找回密码
 立即注册
查看: 9257|回复: 5

[已解决]关于网络异常及网络堵塞URLError: <urlopen error timed out>

[复制链接]
发表于 2016-9-4 10:21:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
URLError: <urlopen error timed out>

我就是想实现的简单功能就是如果网络不行或网络断开重试,即一切网络异常重试,需要加哪些代码,我已加以下代码:
  1.     except socket.timeout, e:  
  2.         print type(e)   
  3.     except urllib2.HTTPError, e:
  4.         print e.code
  5.         print e.reason
复制代码
最佳答案
2016-9-4 15:05:06
  1. #coding:utf8
  2. import urllib2,socket

  3. def getcontent(url,timeout):
  4.     try:
  5.         response=urllib2.urlopen(url,timeout=timeout)
  6.         content = response.read().decode('utf-8')
  7.         return content
  8.     except socket.timeout:
  9.         print "连接超时,将重新尝试连接"
  10.         print timeout
  11.         return getcontent(url,timeout+1)
  12.     except urllib2.HTTPError, e:
  13.         pass
  14. getcontent("http://www.baidu.com",0.01)
复制代码


发了两个帖子半天没人回,也是够了。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-9-4 15:05:06 | 显示全部楼层    本楼为最佳答案   
  1. #coding:utf8
  2. import urllib2,socket

  3. def getcontent(url,timeout):
  4.     try:
  5.         response=urllib2.urlopen(url,timeout=timeout)
  6.         content = response.read().decode('utf-8')
  7.         return content
  8.     except socket.timeout:
  9.         print "连接超时,将重新尝试连接"
  10.         print timeout
  11.         return getcontent(url,timeout+1)
  12.     except urllib2.HTTPError, e:
  13.         pass
  14. getcontent("http://www.baidu.com",0.01)
复制代码


发了两个帖子半天没人回,也是够了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-9-4 17:12:00 | 显示全部楼层
流氓大叔 发表于 2016-9-4 15:05
发了两个帖子半天没人回,也是够了。

不是很理解一定要传二个参数吗,但我二级链接是列表
  1. #正则二级内容
  2. def getcontent_2(url):
  3.     user_agent='Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.04'
  4.     headers={'User-Agent':user_agent}
  5.     try:
  6.         request=urllib2.Request(url,headers = headers)
  7.         response=urllib2.urlopen(request,timeout=5)
  8.         content_2 = response.read().decode('utf-8')
  9.         pattern=re.compile('<a href=".*=u8396862907">.*</a>\'\)</script>\s+</p>\s+</div>\s+<div class="postContent">\s+(.*)\s*<[span]*',re.M)
  10.         contenturl_2=[]
  11.         contenturl_2=re.findall(pattern,content_2)
  12.         for j in contenturl_2:
  13.             pattern_1=re.compile(r'[1-9]([0-9]{5,11})')
  14.             haveqq=re.search(pattern_1,j)
  15.             if haveqq:
  16.                 print j
  17.         response.close()
  18.     except socket.timeout, e:  
  19.         print type(e)     
  20.     except urllib2.HTTPError, e:
  21.         print e.code
  22.         print e.reason

  23.    
  24. #二级链接
  25. def geturl_2(contenturl):
  26.     for k,j in contenturl:
  27.         url=k
  28.       #  print url
  29.         getcontent_2(url)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-9-4 17:16:31 | 显示全部楼层
天冰 发表于 2016-9-4 17:12
不是很理解一定要传二个参数吗,但我二级链接是列表

传两个参数是因为我第一次传入的参数是0.01秒,肯定是会超时返回的,所以我在return getcontent的地方加了一秒,第二次获取内容的时候就变成了1.01秒。如果你设置的时间比如是30秒或者其他的你认为时间差不多的数值,就不必传入两个参数了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-9-4 19:41:23 | 显示全部楼层
流氓大叔 发表于 2016-9-4 17:16
传两个参数是因为我第一次传入的参数是0.01秒,肯定是会超时返回的,所以我在return getcontent的地方加 ...

但时间设置的越大,假死状态就会出现,就是一直卡着不动
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-9-4 21:02:51 | 显示全部楼层
天冰 发表于 2016-9-4 19:41
但时间设置的越大,假死状态就会出现,就是一直卡着不动

那说明访问速度就是这么慢,你要等他返回数据。访问时间设置短了,超时以后重试,你看着是在跑起来了,可是又拿不到想要的数据。嫌慢,看看适不适合换多线程?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-2-22 14:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表