首先找到一个靠谱的排名网站,并利用F12或者右键查询元素找到我们所需数据的存储位置:
在利用pip下载所需的第三方库如:matplotlib等进行编写代码:
import requests
import bs4
from bs4 import BeautifulSoup
import matplotlib.pyplot as plt
url = \'http://m.gaosan.com/gaokao/265440.html\'
r=requests.get(url)
try:
r.raise_for_status()
r.encoding=r.apparent_encoding
data=r.text
except:
print(\'ERROR\')
soup=BeautifulSoup(data,\'html.parser\')
b=[]
for tr in soup.find(\'tbody\').children:
if isinstance(tr,bs4.element.Tag):
tds=tr(\'td\')
b.append([tds[1].string,tds[2].string])
x=[]
y=[]
for i in range(1,20):
x.append(b[i][0])
y.append(b[i][1])
x.reverse()
y.reverse()
plt.barh(range(len(y)),y,tick_label=x,color=\'red\')
plt.rcParams[\'font.sans-serif\'] = [\'STKaiTi\']
plt.rcParams[\'axes.unicode_minus\'] = False
plt.title("中国大学排名及综合得分")
plt.show()
效果如图: