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