美丽汤错误: 名称错误: 未定义名称'htmltext'



我得到这个错误:

NameError: name 'htmltext' is not defined

来自下面的代码:

from bs4 import BeautifulSoup
import urllib
import urllib.parse
url = "http://nytimes.com"
urls = [url]
visited = [url]
while len(urls) > 0:
        try:
           htmltext = urllib.urlopen(urls[0]).read()
        except:
           print(urls[0])      
        soup = BeautifulSoup(htmltext)    
        urls.pop(0)
        print(soup.findAll('a',href = true))

x,你需要导入urllib.request而不是urllib。然后,更改行:

htmltext = urllib.urlopen(urls[0]).read()

:

htmltext = urllib.request.urlopen(urls[0]).read()

最后将true改为True

最新更新