博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【爬虫实战】5Python网络爬虫——中国大学排名定向爬虫
阅读量:3916 次
发布时间:2019-05-23

本文共 3831 字,大约阅读时间需要 12 分钟。

中国大学排名定向爬虫

内容参考自

1、中国大学排名定向爬虫”实例介绍

背景:由,每年对会进行最好大学、最好学科等排名

功能描述:

  • 输入:大学排名URL链接
  • 输出:大学排名信息的屏幕输出(排名,大学名称,总分)
  • 技术路线:requests‐bs4
  • 定向爬虫:仅对输入URL进行爬取,不扩展爬取

定向爬虫可行性

  • F12可以看见
  • https://www.shanghairanking.cn/robots.txt

程序的结构设计:

  • 步骤1:从网络上获取大学排名网页内容——getHTMLText()
  • 步骤2:提取网页内容中信息到合适的数据结构——fillUnivList()
  • 步骤3:利用数据结构展示并输出结果——printUnivList()

2、“中国大学排名定向爬虫”实例编写

对于每一个tbody底下的tr,对应一个大学:

在这里插入图片描述
tr的每一个td对应一个属性:
在这里插入图片描述
问题1:isinstance函数:

意思是“判断类型”;isinstance()是一个内置函数,用于判断一个对象是否是一个已知的类型,类似type()。例如:

a=2isinstance (a,int)		# Trueisinstance (a,str)		# Falseisinstance (a,(str,int,list))    # 是元组中的一个则返回True

问题2:关于输出的格式:

在这里插入图片描述
问题3:运行时报错:

TypeError: unsupported format string passed to NoneType.__format__

放一个例子就懂了:

html = '清华大学'html2 = '清华大学

'html3 = '清华大学
'soup = BeautifulSoup(html,'html.parser')soup2 = BeautifulSoup(html2,'html.parser')soup3 = BeautifulSoup(html3,'html.parser')print(soup.string)print(soup2.string)print(soup3.string)print(soup.text)print(soup2.text)print(soup3.text)

结果:

清华大学NoneNone清华大学清华大学清华大学

分析:

对应的值为None,参考以及,简单地说就是把string换成text就行了

问题4:输出格式问题

输出有很多空格和回车,可使用.strip()函数,如下所示:

str1 = '     \n  abc     \n    d   \n'print(str1)print(str1.strip())   # abc

运行结果如下所示:

abc         d   abc         d

运行很完美,代码如下所示:

import requestsfrom bs4 import BeautifulSoupuinfo = []def getHTMLText(url):    try:        r = requests.get(url, timeout=30)        r.raise_for_status()        r.encoding = r.apparent_encoding        return r.text    except:        print('Error')        return ''def fillUnivList(ulist, html):    import bs4    soup = BeautifulSoup(html, 'html.parser')    for tr in soup.find('tbody').children:        if isinstance(tr, bs4.element.Tag):     # 如果tr确实是一个Tag            tds = tr('td')      # 对于一个大学的所有属性td            ulist.append([tds[0].text.strip(), tds[1].text.strip(), tds[4].text.strip()])def printUnivList(ulist, num):    print("{:^10}\t{:^6}\t{:^10}".format("排名","学校名称","总分"))    for i in range(num):        u = ulist[i]        print("{:^10}\t{:^6}\t{:^10}".format(u[0],u[1],u[2]))def main():    url = 'https://www.shanghairanking.cn/rankings/bcur/2020'    html = getHTMLText(url)    fillUnivList(uinfo, html)    printUnivList(uinfo, 20)  # 20 univsmain()

结果:

排名    	 学校名称 	    总分        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

但还是有点没对齐,下面来解决一下这个问题

中文对齐问题的原因:当中文字符宽度不够时,采用西文字符填充;中西文字符占用宽度不同。解决方法:采用中文字符的空格填充chr(12288)

将printUnivList函数改为以下形式,template中0、1、2、3对应于format的四个属性。

def printUnivList(ulist, num):    tplt = "{0:^10}\t{1:{3}^10}\t{2:^10}"    print(tplt.format("排名","学校名称","总分",chr(12288)))    for i in range(num):        u = ulist[i]        print(tplt.format(u[0],u[1],u[2],chr(12288)))

最后结果:

排名    	   学校名称   	    总分        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   Process finished with exit code 0

最后总分那儿还是感觉很丑……

转载地址:http://jgvrn.baihongyu.com/

你可能感兴趣的文章
JavaScript的一些基础-数据类型
查看>>
JavaScript基础知识(2)
查看>>
转载一个webview开车指南以及实际项目中的使用
查看>>
android中对于非属性动画的整理
查看>>
一个简单的TabLayout的使用
查看>>
ReactNative使用Redux例子
查看>>
Promise的基本使用
查看>>
android给文字加边框(修改不能居中的问题)
查看>>
coursesa课程 Python 3 programming 统计文件有多少单词
查看>>
coursesa课程 Python 3 programming 输出每一行句子的第三个单词
查看>>
coursesa课程 Python 3 programming Dictionary methods 字典的方法
查看>>
Returning a value from a function
查看>>
coursesa课程 Python 3 programming Functions can call other functions 函数调用另一个函数
查看>>
coursesa课程 Python 3 programming The while Statement
查看>>
course_2_assessment_6
查看>>
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
查看>>
coursesa课程 Python 3 programming course_2_assessment_8 sorted练习题
查看>>
visca接口转RS-232C接口线序
查看>>
在unity中建立最小的shader(Minimal Shader)
查看>>
1.3 Debugging of Shaders (调试着色器)
查看>>