之前碰到了一个情况,在抓取网页的过程中,很容易出现错误,我想在出现错误之后再重新尝试一次抓取,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
tries =
0
while
True :
try :
conn =
urllib2.urlopen(url, timeout =
10 )
page =
baidu_conn.read()
conn.close()
break
except
Exception, e:
tries + =
1
print
e
if
tries > = 3 :
page =
‘‘
break
|