解决:name 'requests' is not defined
比如这段代码现在出现NameError: name ‘requests’ is not defined
from bs4 import BeautifulSoup words = ["旗帜", "计算机", "乾坤", "理工"] for word in words: r = requests.get("http://hanyu.baidu.com/s?wd=" + word + "&from=zici") r.encoding = "utf-8" soup = BeautifulSoup(r.text, 'lxml') print("{:-^60}".format(word)) for p in soup.find(id = "basicmean-wrapper").div.dd: print(p.string.strip())
NameError: name 'requests' is not defined
解决方法:
win+r打开cmd
输入pip install requests
>pip install requests
这就成功安好requests了
这里提示更新pip版本就复制过来更新一下就没warning了
>python -m pip install --upgrade pip
进requests包没问题
>python >>import requests >>
再运行下代码也没问题了
作者:龙飞
-------------------------------------------
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!