2015-06-26 82 views
1

我下面一本書的Web使用Python刮,我想這樣的:語法錯誤3.4.3

  • 我在與Python 3.4.3上的虛擬環境OSX
  • BeautifulSoup庫安裝

當我嘗試此:

from urllib.request import urlopen 
from bs4 import BeautifulSoup 
from urllib.error import HTTPError 

html = urlopen("http://www.pythonscraping.com/exercises/exercise1.html") 
except urllib.error.HTTPError as e: 
    print(e.code) 
    if html is none: 
     print("url is not found") 

else: 
    bsObj = BeautifulSoup(html.read()); 
    print(bsObj 

當我跑步時,我甲肝e出現以下錯誤:

(scrapingEnv)Macintosh:scrapingenv nicolas$ python3 scrapetest.py 
    File "scrapetest.py", line 6 
    except urllib.error.HTTPError as e: 
     ^
SyntaxError: invalid syntax 

我也嘗試過使用第6行中的「except urllib.HTTPError」,但沒有任何成功。

我在做什麼錯?

+0

'except'來try'後'。你錯過了代碼的一部分。 https://docs.python.org/2/tutorial/errors.html – Tempux

回答

2

你缺少你try聲明

from urllib.request import urlopen 
from bs4 import BeautifulSoup 
from urllib.error import HTTPError 

try: 
    html = urlopen("http://www.pythonscraping.com/exercises/exercise1.html") 
except urllib.error.HTTPError as e: 
    print(e.code) 
    if html is None: 
     print("url is not found") 

編輯:你也應該改變noneNone