| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
 原题:写一个程序,依次访问文件中指定的站点,并将每个站点返回的内容依次存放到不同的文件中 
这是我的代码 
- import urllib.request as ur
 
  
- def test():
 
 -     count = 1
 
 -     f1 = open('urls.txt', 'r', encoding='utf-8')
 
 -     for each_line in f1:
 
 -         try:
 
 -             response = ur.urlopen(each_line).read()
 
 -             f = open(('url_%s' % count + '.txt'), 'w', encoding='utf-8' )
 
 -             f.write(str(response))
 
 -             f.close()
 
 -         except HTTPError as reason:
 
 -             print('无法访问%s' % each_line)
 
 -     count += 1
 
  
- test()
 
 -         
 
 
  复制代码 
然后我想问一下,这个异常捕获这一块总是出错,URLError和HTTPError都不行,应该怎么改啊~~~ 
是我的问题还是网站的问题呢? 
http://www.fishc.com 
http://www.baidu.com 
http://www.douban.com 
http://www.zhihu.com 
http://www.taobao.com
- import urllib.request as ur
 
 - from urllib.error import HTTPError
 
  
 
- def test():
 
 -     count = 1
 
 -     f1 = open('urls.txt', 'r', encoding='utf-8')
 
 -     for each_line in f1:
 
 -         try:
 
 -             response = ur.urlopen(each_line).read()
 
 -             f = open(('url_%s' % count + '.txt'), 'w', encoding='utf-8')
 
 -             f.write(str(response))
 
 -             f.close()
 
 -         except HTTPError as reason:
 
 -             print('无法访问%s' % each_line)
 
 -     count += 1
 
  
 
- test()
 
 
  复制代码 
 
 
 |   
 
 
 
 |