python爬虫入门爬取lpl选手价值排行榜
生活随笔
收集整理的這篇文章主要介紹了
python爬虫入门爬取lpl选手价值排行榜
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
代碼:
import requests from bs4 import BeautifulSoup import bs4 def getHTMLText(url):try:r = requests.get(url, timeout=30)r.raise_for_status()r.encoding = r.apparent_encodingreturn r.textexcept:return "訪問失敗" def fillUnivList(ulist, ilist,html):soup = bs4.BeautifulSoup(html, "html.parser")for tr in soup.find("tbody").children:if isinstance(tr, bs4.element.Tag):tds = tr('td')ulist.append([tds[0].string, tds[2].string, tds[3].string, tds[4].string, tds[5].string, tds[6].string, tds[7].string])d=soup.find_all('dd')for i in d:ilist+=i def printUnivList(ulist, ilist, num):tplt = "{0:^5}\t{1:^10}\t{2:{8}^30}\t{3:9}\t{4:^9}\t{5:^9}\t{6:^9}\t{7:^9}"print(tplt.format("排名", "選手id", "所屬戰隊", "人氣","輿論","戰績","綜合", "身價",chr(12288)))for i in range(num):u = ulist[i]d=ilistprint(tplt.format(u[0], d[i], u[1], u[2], u[3],u[4],u[5],u[6],chr(12288))) def main():uinfo = []l = []url = 'http://rank.uuu9.com/player/ranking?gameId=2&type=0'html = getHTMLText(url)fillUnivList(uinfo,l, html)printUnivList(uinfo, l, 30) main()結果:
總結
以上是生活随笔為你收集整理的python爬虫入门爬取lpl选手价值排行榜的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 「深度」规模仅为12亿元,充电桩建设真的
- 下一篇: 《Cracking the Coding