哪有什么岁月静好,不过是有人替你负重前行!

解决: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
>>

再运行下代码也没问题了

 

 

 


posted @ 2021-11-14 10:14  longfei825  阅读(1598)  评论(0编辑  收藏  举报