python系列課程,免費python課程排行榜-用python爬取2017年中國最好大學排名

 2023-11-18 阅读 28 评论 0

摘要:爬取2017年中國最好大學排名 在學習中國大學慕課網的python網絡爬蟲與信息提取時,有這么一道題,要求我們爬取2016年的中國最好大學排名鏈接在這,按照題目要求很快便可以爬取到我需要的排名順序。代碼如下 import requests from bs4 import BeautifulSoup

爬取2017年中國最好大學排名

在學習中國大學慕課網的python網絡爬蟲與信息提取時,有這么一道題,要求我們爬取2016年的中國最好大學排名鏈接在這,按照題目要求很快便可以爬取到我需要的排名順序。代碼如下

import requests

from bs4 import BeautifulSoup

import bs4

python系列課程,def getedhtml(url, code='utf-8'):

kv = {'user-agent': 'Mozilla/5.0'}#如果網站有反爬蟲措施,需要改變headers,來偽裝自己

try:

r = requests.get(url, headers=kv, timeout=30)

r.raise_for_status()

r.encoding = code

python爬蟲教程?return r.text

except:

return ''

def returned(html, list, num):

count = 0 #對加入列表中的信息進行計數

soup = BeautifulSoup(html, 'html.parser')

python gui、info = soup.find('tbody', 'hidden_zhpm').children

for tr in info:

if count >= num:

break #如果以及滿足所需要的高校數,就可以退出了

if isinstance(tr, bs4.element.Tag):

count += 1

python編程?tds = tr.find_all('td')

list.append([tds[0].string, tds[1].string, tds[3].string])

def printed(list, num):

print('{0:^10} {1:{3}^10} {2:^10}'.format('排名', '高校', '分數', chr(12288)))

for i in range(num):

L = list[i]

python3?print('{0:^10} {1:{3}^10} {2:^10}'.format(L[0], L[1], L[2], chr(12288)))

def main():

list = []

url = 'http://www.zuihaodaxue.com/zuihaodaxuepaiming2016.html'

num = int(input('請問要查詢2016前多少名的高校呢:'))

html = getedhtml(url)

用python爬取網站數據。returned(html, list, num)

printed(list, num)

main()

但是當我準備繼續爬取2017年的排名時,卻發現程序并沒有如期爬取我要的名次。并給我報出如下錯誤

TypeError: unsupported format string passed to NoneType.__format__

于是我便打開2017年中國最好大學網站,查看其源代碼鏈接

java python。當我鎖定到源代碼中的排名部分時,發現了問題所在。

在圖中的排名子節點"1”所對應的地方并非是一個

標簽所包圍的所有內容,而這相當于把后續所有內容全給裝進去了,所以肯定沒有辦法對其進行string操作,來獲得其排名。

由于字符1仍是第一的

標簽下的第一個子節點,所以我打算通過bs4庫的contents方法來獲得這個排名,由于是第一個子節點,那么tds[0].contents[0]就是我們所需要的排名。重新修改的代碼如下

# -*- coding: utf-8 -*-

import requests

from bs4 import BeautifulSoup

大學Python、import bs4

def getedhtml(url, code='utf-8'):

kv = {'user-agent': 'Mozilla/5.0'}#網站有反爬蟲措施,需要改變headers,來偽裝自己

try:

r = requests.get(url, headers=kv, timeout=30)

r. raise_for_status()

vs2017怎么安裝python,r.encoding = code

return r.text

except:

return ''

def returned(html, list, num):

count = 0 #對加入列表中的信息進行計數

vs2017python開發。soup = BeautifulSoup(html, 'html.parser')

info = soup.find('tbody', 'hidden_zhpm').children

for tr in info:

if count >= num:

break #如果以及滿足所需要的高校數,就可以退出了

if isinstance(tr, bs4.element.Tag):

Python爬取?count += 1

tds = tr.find_all('td')

list.append([tds[0].contents[0], tds[1].string, tds[3].string])

def printed(list, num):

print('{0:^10} {1:{3}^10} {2:^10}'.format('排名', '高校', '分數', chr(12288)))

for i in range(num):

大學哪個專業學python。L = list[i]

print('{0:^10} {1:{3}^10} {2:^10}'.format(L[0], L[1], L[2], chr(12288)))

def main():

list = []

url = 'http://www.zuihaodaxue.com/zuihaodaxuepaiming2017.html'

num = int(input('請問要查詢2017前多少名的高校呢:'))

python課程推薦。html = getedhtml(url)

returned(html, list, num)

printed(list, num)

main()

運行所寫代碼

排名     高校     分數

考研考python的學校,1    清華大學    94.0

2    北京大學    81.2

3    浙江大學    77.8

4   上海交通大學   77.5

5    復旦大學    71.1

6  中國科學技術大學  65.9

vs2015可以寫python嗎。7    南京大學    65.3

8   華中科技大學   63.0

9    中山大學    62.7

10  哈爾濱工業大學   61.6

11    同濟大學    60.8

12    東南大學    59.8

學Python爬蟲選哪本書最好?13    武漢大學    58.4

14  北京航空航天大學  58.3

15    南開大學    58.2

16    四川大學    57.4

16   西安交通大學   57.4

18    天津大學    56.2

pycharm和python區別,19   華南理工大學   56.1

20   北京師范大學   55.1

結果如下,成功拿到排名數據。

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/177111.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息