首页 > 开发 > Python > 正文

python 如何长时间监控一个网站上的数据,并保持不断线

2017-09-06 19:11:20  来源:网友分享

需求:需要长时间对某个网站上的数据进行监控

1、目前采用的方法是通过urllib2.urlopen获取网站源代码,然后正则匹配想要的数据

2、最后通过while True:循环不断访问网站获取信息

遇到的问题:运行时间一长,就难免会出现urllib2.HTTPErroe:HTTP Error 500:Internal Server Error,然后就会中断运行,不知道如何避免因为网络问题而引起的中断?

解决方案

我的做法是,

while True:    try:        """        代码逻辑        """        time.sleep(many time) # 停一段时间    except Exception as e:        print(e) # 或者是写进日志        continue