ImportError: No module named 'requests',No module named 'Bs4'(找不到requests模块)

版权声明:本文为博主原创文章,转载请注明原地址。 https://blog.csdn.net/a1023182899/article/details/82669781

安装了python3.5的环境后,一个网络爬虫python文件运行的时候,代码在下面,
提示:
ImportError: No module named ‘requests’(找不到requests模块)
ImportError: No module named ‘Bs4’(找不到Bs4模块)

requests介绍:requests是python的一个HTTP客户端库,跟urllib,urllib2类似,那为什么要用requests而不用urllib2呢?

官方文档中是这样说明的:python的标准库urllib2提供了大部分需要的HTTP功能,但是API太逆天了,一个简单的功能就需要一大堆代码。

解决方法:
我安装的python的时候,选择安装了pip,所以这里可以,找到python安装的目录,cd进去。

①cmd
②cd D:\Need_to_install\Python
③pip install requests
等待系统自动加载安装。
④cd D:\Need_to_install\Python
⑤pip install bs4
等待系统自动加载安装。

网络爬虫代码如下:

#e23.1CrawUnivRanking.py
import requests
from bs4 import BeautifulSoup
allUniv = []
def getHTMLText(url):
    try:
        r = requests.get(url, timeout=30)
        r.raise_for_status()
        r.encoding = 'utf-8'
        return r.text
    except:
        return ""
def fillUnivList(soup):
    data = soup.find_all('tr')
    for tr in data:
        ltd = tr.find_all('td')
        if len(ltd)==0:
            continue
        singleUniv = []
        for td in ltd:
            singleUniv.append(td.string)
        allUniv.append(singleUniv)
def printUnivList(num):
    print("{:^4}{:^10}{:^5}{:^8}{:^10}".format("排名","学校名称","省市","总分","培养规模"))
    for i in range(num):
        u=allUniv[i]
        print("{:^4}{:^10}{:^5}{:^8}{:^10}".format(u[0],u[1],u[2],u[3],u[6]))
def main():
    url = 'http://www.zuihaodaxue.cn/zuihaodaxuepaiming2016.html'
    html = getHTMLText(url)
    soup = BeautifulSoup(html, "html.parser")
    fillUnivList(soup)
    printUnivList(10)
main()

猜你喜欢

转载自blog.csdn.net/a1023182899/article/details/82669781
今日推荐