天天看點

python爬取28萬足球運動員資料,告訴你中國隊為什麼不能出線一、資料抓取二、分析資料

中國隊又沒能在世界杯中出線,對此我準備從球員層面探求一下原因,意圖通過資料分析拯救中國足球.

一、資料抓取

使用python暴力抓取懂球帝上的球員資料,如下圖:

python爬取28萬足球運動員資料,告訴你中國隊為什麼不能出線一、資料抓取二、分析資料

主要包括球員基本資訊,以及各項能力得分。

import requests
import time
import pymysql
from bs4 import BeautifulSoup

user_agent = 'Your user_agent'
headers = {'User-Agent':user_agent}

coon = pymysql.connect(
    host='localhost',
    user='root',
    password='123456',
    db='tset',
    port=3306
)
cur = coon.cursor()
sql = """
    insert into player_test (cn_name,en_name,player_img,club,pos,num,country,age,birthday,height,weight,foot,score,speed,power,defense,dribble,pass_ball,shoot)
                VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
"""

url = 'https://www.dongqiudi.com/player/50025255.html'
html = requests.get(url=url, headers=headers)
soup = BeautifulSoup(html.text, 'lxml')
img_src = soup.find_all('img')[1].attrs['src']

if img_src == 'https://static1.dongqiudi.com/web-new/web/images/icon_error.png':
    pass
else:
    # 個人資訊
    cn_name = soup.h1.text  # 姓名
    en_name = soup.find('span',class_='en_name').text  # 英文名
    player_img = soup.find('img',class_='player_img').attrs['src']  # 頭像位址
    detail_info = soup.find('ul',class_='detail_info').find_all('li')

    club = detail_info[0].text.split(':')[1].strip()  # 俱樂部
    pos = detail_info[1].text.split(':')[1].strip()  # 場上位置
    num = detail_info[2].text.split(':')[1].replace('号', '').strip()  # 号碼
    if num == '':
        num = 0
    else:
        num = int(num)
    country = detail_info[3].text.split(':')[1].strip()  # 國家
    age = detail_info[4].text.split(':')[1].replace('歲', '')  # 年齡
    birthday = detail_info[5].text.split(':')[1]  # 出生日期
    height = detail_info[6].text.split(':')[1].replace('CM', '').strip()  # 身高
    if height == '':
        height = 0
    else:
        height = float(height)
    weight = detail_info[7].text.split(':')[1].replace('KG', '').strip()  # 體重
    if weight == '':
        weight = 0
    else:
        weight = float(height)
    foot = detail_info[8].text.split(':')[1].replace('腳', '').strip()  # 慣用腳

    # 綜合能力
    score = soup.find('div', id='title').text.replace('綜合能力', '').strip()  # 綜合得分
    if score == '':
        score = 0
    else:
        score = int(weight)
    speed = soup.find('div', class_='item item0').find('span').text.strip()
    if speed == '':
        speed = 0
    else:
        speed = int(weight)
    power = soup.find('div', class_='item item1').find('span').text.strip()
    if power == '':
        power = 0
    else:
        power = int(weight)
    defense = soup.find('div', class_='item item2').find('span').text.strip()
    if defense == '':
        defense = 0
    else:
        defense = int(weight)
    dribble = soup.find('div', class_='item item3').find('span').text.strip()
    if dribble == '':
        dribble = 0
    else:
        dribble = int(weight)
    pass_ball = soup.find('div', class_='item item4').find('span').text.strip()
    if pass_ball == '':
        pass_ball = 0
    else:
        pass_ball = int(weight)
    shoot = soup.find('div', class_='item item5').find('span').text.strip()
    if shoot == '':
        shoot = 0
    else:
        shoot = int(weight)
    cur.execute(sql, (cn_name,en_name,player_img,club,pos,num,country,age,birthday,height,weight,foot,score,speed,power,defense,dribble,pass_ball,shoot))
    coon.commit()

coon.close()

           

爬蟲過程中,值得注意的一點是,入庫的時候有很多我在爬蟲結果中看不到的空格和換行符,是以,為了資料的整潔,資料入庫之前都要做一次去空格的處理,當然,在資料分析時使用pandas也可以。

二、分析資料

本次使用pandas進行資料分析,首先要連接配接資料庫:

import pandas as pd
import os
import pymysql
from sqlalchemy import create_engine

pd.set_option('max_columns',1000) 
engine = create_engine('mysql+pymysql://root:[email protected]:3306/tset?charset=utf8')
sql = 'select * from player_info'
df = pd.read_sql(sql=sql, con=engine)
df.head()
           

結果:

python爬取28萬足球運動員資料,告訴你中國隊為什麼不能出線一、資料抓取二、分析資料

這樣就得到了資料。

我們首先來看一下,各個國家現代足球發展的早晚和國足在世界範圍内的排名有什麼關系。

為了友善,我們以球員名單中,每個國家最高出生的球員作為該國家現代足球開始的時間,

python爬取28萬足球運動員資料,告訴你中國隊為什麼不能出線一、資料抓取二、分析資料

令人意想不到的是第一個足球運動員竟然是美國人,出生時間是1873年,那麼我們假設他25歲成名,那麼現代足球開始的時間應該是1898年。除此之外,我們還可以發現,現代足球開始時間較早的基本都是歐洲及南美國家。那麼中國是什麼時候開始的呢,我們看一下`

df_ranks.loc[df_ranks['country']=='中國']
           
python爬取28萬足球運動員資料,告訴你中國隊為什麼不能出線一、資料抓取二、分析資料

中國排在第76位,假設中國第一位足球運動員也是25歲成名,那麼中國現代足球開始的時間是1969年,新中國剛剛成立20年。整整落後世界排名第一的美國29年。是以說,努力要趁早啊。

我們接着再從球員數量方面來看一下:

player_nums = df_player_nums.rename(columns={'id':'player_nums'})
player_nums.head(30)
           
python爬取28萬足球運動員資料,告訴你中國隊為什麼不能出線一、資料抓取二、分析資料

看一下各國球員數量,超過一萬人的國家有巴西、法國、英格蘭、阿根廷,巴西以16900人次穩居榜首,也難怪各個國家的聯賽都有巴西人的身影,遠的不說,就說中超聯賽,各個俱樂部的大腿基本都是巴西人,上海上港的胡爾克、奧斯卡、埃爾克森,廣州恒大的塔利斯卡、保利尼奧、阿蘭、高拉特,天津權健的帕托…

再看看中國呢:

python爬取28萬足球運動員資料,告訴你中國隊為什麼不能出線一、資料抓取二、分析資料

還不錯,中國登記在冊的球員有1562人,排名世界43位。那麼從球員數量來說,中國現在的成績不能令人接受啊,是以,發展青訓,多多培養年輕球員才是王道,現在就像現在的國足一樣,就那麼幾個人,踢來踢去還是那麼幾個人,表現怎麼樣都能入選國家隊,怎麼出成績,要知道,皇馬當家前鋒‘本澤馬’都入選不了法國國家隊啊,歸根結底還是人家球員多,可選擇的也多。有人說,我泱泱大國還挑不出幾個能踢球的?不是人多就行,還得看能踢球的人又多少。

下面我們來看一下,各國球員數量占總人口的比例:

python爬取28萬足球運動員資料,告訴你中國隊為什麼不能出線一、資料抓取二、分析資料

我們來看,排名第一的是聖馬力諾,位于歐洲南部,整個國家被意大利包圍,整個國家三萬多人,月474名球員,每萬人就有141個人是足球運動員。

再來看一下中國,

python爬取28萬足球運動員資料,告訴你中國隊為什麼不能出線一、資料抓取二、分析資料

真的是泱泱大國,13億8千萬人口,1562名球員,每百萬人才有一個足球運動員。放眼整個世界範圍内,僅僅高于印度和巴基斯坦,不過還好,總算沒有墊底,哎,等等,我記得前幾天國足好像頑強的逼平了印度…

具體的其他特征,比如說各個位置上的球員、球員身高體重、得分等等,我就不一一分析了,看一下這些資料真的很有意思,想要這份資料的同學可以給我留言。