一、功能描述及程序设计
二、代码实现
1 """中国大学排名定向爬虫实例介绍""" 2 3 import requests 4 from bs4 import BeautifulSoup 5 import bs4 6 7 8 def getHTMLTest(url): 9 10 try: 11 r = requests.get(url, timeout=30) 12 r.raise_for_status() 13 r.encoding = r.apparent_encoding 14 return r.text 15 except: 16 return "getHTMLTest错误" 17 18 19 def fillUnivList(ulist, html): 20 21 soup = BeautifulSoup(html, "html.parser") 22 for tr in soup.find('tbody').children: 23 # 判断标签是否为bs4.element.Tag 24 if isinstance(tr, bs4.element.Tag): 25 tds = tr('td') 26 # 获得每一个td标签的string值 27 ulist.append([tds[0].string, tds[1].string, tds[2].string, tds[3].string]) 28 29 30 def printUnivList(ulist, num): 31 32 print("{:^10} {:^6} {:^10} {:^10}".format("排名", "学校名称", "省市", "总分")) 33 for i in range(num): 34 u = ulist[i] 35 print("{:^10} {:^6} {:^10} {:^10}".format(u[0], u[1], u[2], u[3])) 36 37 38 if __name__ == "__main__": 39 uinfo = [] 40 url = "http://www.zuihaodaxue.cn/zuihaodaxuepaiming2016.html" 41 html = getHTMLTest(url) 42 fillUnivList(uinfo, html) 43 printUnivList(uinfo, 20) # 20代表输出学校数
三、对输出函数优化
1、问题原因
2、解决方法:
填充时指定用中文字符进行填充,chr(12288)为中文空格
def printUnivList2(ulist, num): # 定义一个输出模板 tplt = "{0:^10} {1:{4}^10} {2:^10} {3:^10}" # {4}代表用第4个值填充 print(tplt.format("排名", "学校名称", "省市", "总分", chr(12288))) # chr(12288)代表中文空格 for i in range(num): u = ulist[i] print(tplt.format(u[0], u[1], u[2], u[3], chr(12288)))