Python 3.4.3中的NameError

时间:2015-12-07 21:51:52

标签: python beautifulsoup syntax-error

当我尝试使用以下配置时:

  • 使用python3.4.3的VirtualEnv
  • 在线IDE上运行

当我尝试这个时:

Sequence: 1
GroupValue: 00000000000100

我收到以下错误:

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)

我做错了什么?

1 个答案:

答案 0 :(得分:1)

感谢您的提示,我找到了解决问题的方法:

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)