python 3.4.3中urllib.httperror上的语法错误

时间:2015-06-26 02:07:18

标签: python syntax-error urllib python-3.4

我正在阅读一本关于Web Scraping with Python的书,我试着这样做:

  • 我在OSX上使用python 3.4.3进行虚拟环境
  • 已安装BeautifulSoup库

当我尝试这个时:

   SELECT TOP 1000 *
   FROM [tisonline].[dbo].[Jobs] AS j
   LEFT OUTER JOIN [tisonline].dbo.JobQueries AS jq 
   ON j.JobID = jq.JobID 
   LEFT OUTER JOIN [tisonline]. dbo.Agents AS agt ON agt.AgentID = jq.AgentID
   where j.isMigrated = 1
   OR (j.isMigrated = 0 AND jp.JobID IS NOT NULL AND agt.AgentID IS NOT NULL)

当我运行它时,我有以下错误:

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

我也试过"除了urllib.HTTPError"在第6行没有任何成功。

我做错了什么?

1 个答案:

答案 0 :(得分: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")

修改:您还应将none更改为None