Home > other > The crawler couldn't print the information
The crawler couldn't print the information
Time:09-18
Chung day in class for the teacher of the course, according to his code written reptile, but can't print any information, could you tell me who is this what went wrong? # CrawUnivRankingA. Py The import requests The from bs4 import BeautifulSoup The import bs4
Def getHTMLText (url) : Try: R=requests. Get (url, timeout=30) R.r aise_for_status () R.e ncoding=of state Richard armitage pparent_encoding Return r.t ext Except: Return ""
Def fillUnivList (ulist, HTML) : Soup=BeautifulSoup (HTML, "HTML parser") For the tr in soup. The find (' tbody.) children: If isinstance (tr, bs4. Element. The Tag) : TDS=tr (" td ") Ulist. Append ([TDS [0], string, TDS [1]. The string, TDS [3]. The string])
Def printUnivList (ulist, num) : Print (" {: ^ 10} {: ^ 6} \ \ t t {: ^ 10} ". The format (" rank ", "school name", "total")) For I in range (num) : U=ulist [I] Print (" {: ^ 10} {: ^ 6} \ \ t t {: ^ 10} ". The format (u [0], u [1], u [2]))
Def the main () : Uinfo=[] Url='https://www.zuihaodaxue.cn/zuihaodaxuepaiming2016.html' HTML=getHTMLText (url) FillUnivList (uinfo, HTML) PrintUnivList (20) uinfo, # 20 univs