python - 安装请求后(使用 JupyterLab),获取 "NameError: name ' 请求未定义”

标签 python beautifulsoup nameerror

我已经使用'pip install requests'安装了请求,但我没有得到任何返回。

我正在遵循基本 tutorial 中给出的示例并在此代码上不断收到此错误:

from bs4 import BeautifulSoup
page = requests.get('https://forecast.weather.gov/MapClick.php?lat=47.19044000000008&lon=-122.29563999999999#.Xq3cLZl7lPY')
soup = BeautifulSoup(page.content, 'html-parser')
print(soup)

错误:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-90e58007c1ee> in <module>
      1 from bs4 import BeautifulSoup
----> 2 page = requests.get('https://forecast.weather.gov/MapClick.php?lat=47.19044000000008&lon=-122.29563999999999#.Xq3cLZl7lPY')
      3 soup = BeautifulSoup(page.content, 'html-parser')
      4 print(soup)

NameError: name 'requests' is not defined

最佳答案

我认为,您需要导入请求。你可以尝试一下:

from bs4 import BeautifulSoup
import requests
page = requests.get('https://forecast.weather.gov/MapClick.php?lat=47.19044000000008&lon=-122.29563999999999#.Xq3cLZl7lPY')
soup = BeautifulSoup(page.text, 'lxml')
print(soup)

关于python - 安装请求后(使用 JupyterLab),获取 "NameError: name ' 请求未定义”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61566552/

相关文章:

Python:NameError:未定义全局名称 'foobar'

python - 全局名称 'get_user_model' 未定义

python - NameError:全局名称 'NAME' 未定义

python - 使用 opencv - python 检测 W2 中的单个框

python - 如何在GroupBy对象的每个组中删除某个索引处的行?

python - 如何从html标签之间提取文本?

python - 带/不带类和字符串的 p 标签

python - 无法从某些 html 元素中获取文本的特定部分

python - linux python numpy脚本在shell中运行但不在终端中运行

python创建名为temporaryfile的临时文件并在其上调用子进程