Python 名称错误:名称';bs4和x27;没有定义
当我运行代码时:Python 名称错误:名称';bs4和x27;没有定义,python,beautifulsoup,urllib,Python,Beautifulsoup,Urllib,当我运行代码时: import requests from bs4 import BeautifulSoup import urllib response = urllib.urlopen('file:///Users/kerss/diet/sesame_seeds.html') html = response.read() soup = bs4.BeautifulSoup(html, 'html.parser') span = soup.find("span", id="NUTRIENT_
import requests
from bs4 import BeautifulSoup
import urllib
response = urllib.urlopen('file:///Users/kerss/diet/sesame_seeds.html')
html = response.read()
soup = bs4.BeautifulSoup(html, 'html.parser')
span = soup.find("span", id="NUTRIENT_0")
print(span.text)
我得到以下错误:
File "c:\users\kerss\diet\scrape.py", line 8, in <module>
soup = bs4.BeautifulSoup(html, 'html.parser')
NameError: name 'bs4' is not defined
文件“c:\users\kerss\diet\scrape.py”,第8行,在
soup=bs4.BeautifulSoup(html'html.parser')
NameError:未定义名称“bs4”
但是bs4被定义了吗?是否?只需使用
soup=beautifulsop(html,'html.parser')
将从bs4导入beautifulsop
更改为导入bs4
只需使用soup=beautifulsop(html,'html.parser')
您是不是碰巧从其他地方复制了这一行?你检查过那个代码的导入吗?