爬取软科中国最好大学排名

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39071593/article/details/83352325

在这里插入图片描述
作为一个刚学会使用BeautifulSoup的菜鸟我二话不说直接把align='left’属性作为它们共同特点,敲了两下代码顺便复习了一下python基本语法,果不其然一下就爬下来了。

import requests
from bs4 import BeautifulSoup

headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
} #伪装成360浏览器
url = 'http://www.zuihaodaxue.com/zuihaodaxuepaiming2018.html' 
html = requests.get(url,headers = headers)
html.encoding = html.apparent_encoding
soup = BeautifulSoup(html.text,'lxml')
a = soup.body
b = a.find_all(attrs={'align': 'left'})   #找到所有标签带有align='left'属性的标签
num = 1
for div in b:
    print("No.%s %s" %(num,div.string))   #输出找到标签的内容也就是大学名字
    num += 1

有图有真相
结果:
在这里插入图片描述
其中最实用的是 b = a.find_all(attrs={‘align’: ‘left’}) 直接定位在带有学校名的标签在用’.string’输出标签内的内容

猜你喜欢

转载自blog.csdn.net/qq_39071593/article/details/83352325