1. 程式人生 > 其它 >解決:name 'requests' is not defined

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

再執行下程式碼也沒問題了




    作者:龍飛
出處:
http://www.cnblogs.com/longfei825/

-------------------------------------------

個性簽名:獨學而無友,則孤陋而寡聞。做一個靈魂有趣的人!

如果覺得這篇文章對你有小小的幫助的話,記得在右下角點個“推薦”哦,博主在此感謝!