我在 C:\webpage.htm 位置保存了一个网页。我想加载它并使用 BeautifulSoup 分析它,但是 urllib 不会打开它。
from BeautifulSoup import BeautifulSoup
import urllib2
url="C:\webpage.htm"
page=urllib2.urlopen(url)
这会引发错误:
Traceback (most recent call last):
page=urllib2.urlopen(url)
File "C:\Python27\lib\urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "C:\Python27\lib\urllib2.py", line 400, in open
response = self._open(req, data)
File "C:\Python27\lib\urllib2.py", line 423, in _open
'unknown_open', req)
File "C:\Python27\lib\urllib2.py", line 378, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 1240, in unknown_open
raise URLError('unknown url type: %s' % type)
urllib2.URLError: <urlopen error unknown url type: c>
我该如何解决这个问题,或者是否有另一种方法可以将文档加载到漂亮的汤中(我曾尝试将其保存为文本文档,但抛出了错误:
'str' object has no attribute 'findall'