Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 名称错误:名称';bs4和x27;没有定义_Python_Beautifulsoup_Urllib - Fatal编程技术网

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')
您是不是碰巧从其他地方复制了这一行?你检查过那个代码的导入吗?