比如這段代碼現在出現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 >>
再運行下代碼也沒問題了