解决:name 'requests' is not defined

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

解决:name 'requests' is not defined_第1张图片

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_第2张图片
这就成功安好requests了

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

>python -m pip install --upgrade pip

进python康康导requests包没问题

>python
>>import requests
>>

再运行下代码也没问题了
解决:name 'requests' is not defined_第3张图片

你可能感兴趣的:(大数据处理与分析,python,爬虫)