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

再运行下代码也没问题了

 

 

 




					


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM