Python 3.4.3中的NameError
当我使用以下配置尝试此操作时:Python 3.4.3中的NameError,python,beautifulsoup,syntax-error,Python,Beautifulsoup,Syntax Error,当我使用以下配置尝试此操作时: 带python3.4.3的VirtualEnv 在在线IDE上运行 当我尝试这个时: from urllib.request import urlopen from urllib.error import HTTPError from bs4 import BeautifulSoup try: html = urlopen("http://www.pythonscraping.com/pages/pages1.html") if html is No
- 带python3.4.3的VirtualEnv
- 在在线IDE上运行
from urllib.request import urlopen
from urllib.error import HTTPError
from bs4 import BeautifulSoup
try:
html = urlopen("http://www.pythonscraping.com/pages/pages1.html")
if html is None:
print("url not found")
else:
except HTTPError as e:
print("test")
else:
bsObj = BeautifulSoup(html.read())
print(bsObj)
我得到了以下错误:
~/workspace/scrapingEnv $ python test2.py
File "test2.py", line 7
if html is None:
^
SyntaxError: invalid syntax
我做错了什么?谢谢你的提示,我找到了解决问题的方法:
from urllib.request import urlopen
from urllib.error import HTTPError
from urllib.error import URLError
from bs4 import BeautifulSoup
try:
html = urlopen("http://www.pythonscrapng.com/pages/pages1.html")
bsObj = BeautifulSoup(html.read())
print(bsObj)
except HTTPError as e:
print("test")
except URLError as j:
print ("No URL")
else:
bsObj = BeautifulSoup(html.read())
print(bsObj)
除了和如果/
如果
/否则
嵌套得很奇怪,缩进在Python中很重要,如果缩进是文件格式的准确表示,则它在Python中无效。