解决:name 'requests' is not defined

比如这段代码现在出现NameError: name ‘requests’ is not defined

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

解决:name 'requests' is not defined

这就成功安好requests了

这里提示更新pip版本就复制过来更新一下就没warning了

>python -m pip install --upgrade pip

进requests包没问题

>python

>>import requests
>>

再运行下代码也没问题了

解决:name 'requests' is not defined

 

 

 


上一篇:【无标题】


下一篇:Qt之启动外部程序