是这样吧?
"""世界大学排名"""
import requests
from bs4 import BeautifulSoup
import bs4
def getHTMLText(url):
try:
r = requests.get(url, timeout=30, headers={"User-Agent": "Mozilla/5.0"})
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except Exception:
return ""
def fillMovieList(ulist, html):
soup = BeautifulSoup(html, "html.parser")
for div in soup.find('div', class_="content"):
if isinstance(div, bs4.element.Tag):
ulist.append(div.string)
else:
return ""
def printMovieList(ulist, num):
tplt = "{:^10}\t"
print(tplt.format("大学名称"))
for i in range(num):
u = ulist[i]
print(tplt.format(u))
def main():
uinfo = []
url = 'http://www.unjs.com/z/1646082.html'
html = getHTMLText(url)
fillMovieList(uinfo, html)
printMovieList(uinfo, 10)
main()
|