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_encoding
        return r.text
    except:
        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=ilist
        print(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选手价值排行榜_第1张图片
python爬虫入门爬取lpl选手价值排行榜_第2张图片

你可能感兴趣的:(python,url)