简体   繁体   English

Urllib2中的异常引发-Python

[英]Exception Raising in Urllib2 - Python

I'm trying to use the following code to check whether a given site is online: 我正在尝试使用以下代码检查给定站点是否在线:

url_list = ["www.apple.com", "www.invalidurlname.com"]
for i in url_list:
    try:
        urllib2.urlopen(i, timeout = 1)
    except urllib2.URLError:
        print "fail"
    else:
        print "pass"

It should produce: 它应该产生:

pass
fail

However when I run it I get the following error: 但是,当我运行它时,出现以下错误:

  File "scanpb.py", line 7
urllib2.urlopen("http://apple.com", timeout = 1)
      ^
IndentationError: expected an indented block

Where have I gone wrong? 我哪里出问题了? Thanks. 谢谢。

You mixed tabs and spaces (displaying tabs as tab and spaces as . : 您混用了制表符和空格(将制表符显示为tab符,并将空格显示为.

tab try:
....tab urllib2.urlopen(i, timeout = 1)

Set your editor to either one, and use your editor's replace function to replace all occurences (you can match tabs with \\t in regex mode). 将您的编辑器设置为任一,然后使用编辑器的替换功能替换所有出现的内容(您可以在正则表达式模式下将选项卡与\\t匹配)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM