success=0#抓取数
error=open('error.txt','a')
f = open('download_1.txt', 'a')
for line in open('URL.txt'):
try:
url=line
soup = BeautifulSoup(getHTML(url), 'lxml')
file_write()
success+=1
print('已成功抓取%d个,'%success+'url为:'+url)
except:
success+=1
print('第%d'%success+'个失败,url为:'+url+'写入error.txt')
error.write(url+'\n')
continue
f.close()
error.close()
如上,出现异常后,我想将出错的url写入error文件,出现异常后可以print,但是error.txt中没有数据如何实现?
|
|
|
|
|
共 1 个关于本帖的回复 最后回复于 2017-5-2 10:47