Python Best University Network University Ranking Crawl (2020)

In Teacher Song's course, he used the BeautifulSoup library to crawl the best university network and university ranking courses. The crawled web pages were links from 2016. Now the website domain name and web pages of the best university network have undergone some changes. In the teacher's original code Some modifications have been made based on it, and the latest crawling code is given.

1. Web page analysis

Web link: https://www.shanghairanking.cn/rankings/bcur/2020

What has changed: The university name now has a hyperlink, so the university name is wrapped in the a tag under the td tag. This needs attention.

2. The code is as follows:

import requests
import re
from bs4 import BeautifulSoup
import bs4


# 获取目标网址的文本信息
def getHtmlText(url):
    try:
        r = requests.get(url)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except:
        return "获取文本异常"


# 通过BeautifulSoup库解析网页,将需要的信息加入到一个列表中
def jiexi(ulist, demo):
    soup = BeautifulSoup(demo, "html.parser")
    for tr in soup.find("tbody").children:
        if isinstance(tr, bs4.element.Tag):  # 判断tr标签是不是bs4定义的tag标签,过滤掉其它的
            tds = tr.find_all('td')  # 将td标签内容加入到tds列表中
            aa = tds[1].find("a").string  # 因为大学名字在td标签的子标签a中,所以需要单独提取
            ulist.append([tds[0].string, aa, tds[4].string])


def printUlist(ulist, num):
    tplt = "{0:^10}{1:{3}^10}{2:^10}"
    print(tplt.format("序号", "学校名称", "分数", chr(12288))) #中文空格填充,能保证输出对齐
    for i in range(num):
        u = ulist[i]
        a = u[0].strip()  # 去掉字符串类两边的空格
        b = u[1].strip()
        c = u[2].strip()
        print(tplt.format(a, b, c, chr(12288)))


def main():
    url = "https://www.shanghairanking.cn/rankings/bcur/2020"
    ulist = []
    demo = getHtmlText(url)
    jiexi(ulist, demo)
    printUlist(ulist, 50)


main()

 Output result:

序号       学校名称       分数    
    1        清华大学     852.5   
    2        北京大学     746.7   
    3        浙江大学     649.2   
    4       上海交通大学    625.9   
    5        南京大学     566.1   
    6        复旦大学     556.7   
    7      中国科学技术大学   526.4   
    8       华中科技大学    497.7   
    9        武汉大学      488    
    10       中山大学     457.2   
    11      西安交通大学    452.5   
    12     哈尔滨工业大学    450.2   
    13     北京航空航天大学   445.1   
    14      北京师范大学    440.9   
    15       同济大学      439    
    16       四川大学     435.7   
    17       东南大学     432.7   
    18      中国人民大学    409.7   
    19       南开大学     402.1   
    20      北京理工大学    395.6   
    21       天津大学     390.3   
    22       山东大学     387.9   
    23       厦门大学     383.3   
    24       吉林大学     379.5   
    25      华南理工大学    379.4   
    26       中南大学     378.6   
    27      大连理工大学    365.1   
    28      西北工业大学    359.6   
    29      华东师范大学     358    
    30      中国农业大学    351.5   
    31       湖南大学     348.3   
    32      电子科技大学    334.8   
    33      北京科技大学    321.8   
    34       重庆大学     320.9   
    35     南京航空航天大学   319.5   
    36      南京理工大学    317.1   
    37       东北大学      314    
    38       苏州大学     306.8   
    39      华中农业大学    300.5   
    40       兰州大学     300.4   
    41     西安电子科技大学   299.4   
    42      华东理工大学    294.6   
    43      北京交通大学    293.6   
    44      华中师范大学    292.4   
    45       上海大学      290    
    46      南方科技大学     289    
    47      南京农业大学    283.2   
    48       暨南大学     282.6   
    49      中国海洋大学    281.2   
    50      南京师范大学    279.9  

 

Guess you like

Origin blog.csdn.net/weixin_47401101/article/details/109532540#comments_28236820