| 
                        副标题[/!--empirenews.page--]
                         看了火箭和勇士的G6大战,最终火箭3比4出局。 
在火箭的近五年季后赛,一共有四次是和勇士交手,最终都以失败告终。 
我平常是很少看NBA比赛的,所以看完之后便想写点东西。 
怀念一下那些年我们一起关注过,当然也打过的篮球。 
涉及的东西不多,主要是NBA球员历年薪资情况以及效力球队情况。 
这里的球员都是NBA的篮球巨星。 
至于超巨是哪几个,估摸着大家各自都有各自的想法啦。 
我也不多说~ 
一、获取分析 
数据从下面这个网站上获取,获取热门球员信息。 
详情见下图。 
  
这里以詹姆斯为例,首先查看他的网址ID。 
  
接下来进入球员数据概况详情页。 
查看他各赛季的薪资以及效力球队情况。 
  
获取赛季、球队以及薪金数据。 
  
二、数据获取 
具体代码如下。 
- import requests 
 - from bs4 import BeautifulSoup 
 -  
 - headers = { 
 -     'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36' 
 - } 
 -  
 -  
 - def get_player(player_id): 
 -     url_2 = 'http://www.stat-nba.com' + player_id 
 -     response_2 = requests.get(url=url_2, headers=headers) 
 -     html_2 = response_2.text 
 -     # 对请求结果进行编码解码处理,避免出现乱码 
 -     html_2 = html_2.encode('ISO-8859-1') 
 -     html_2 = html_2.decode('utf-8') 
 -     # 提取信息 
 -     soup_2 = BeautifulSoup(html_2, 'html.parser') 
 -     name = soup_2.find(class_='name').get_text().split('/')[0] 
 -     salarys = soup_2.find(id='player_salary') 
 -     trs = salarys.find('tbody') 
 -     # 获取信息 
 -     for tr in trs.find_all('tr')[:-2]: 
 -         # 年份 
 -         season = tr.find(class_='current').get_text() 
 -         if int(season[:1]) > 2: 
 -             year = '19' + season.split('-')[0] 
 -         else: 
 -             year = '20' + season.split('-')[0] 
 -         # 球队 
 -         team = tr.find('a').get_text() 
 -         # 薪水 
 -         salary = tr.find_all(class_='normal')[1].get_text().replace('万美元', '') 
 -         print(year, name, team, salary) 
 -         with open('nba.csv', 'a+', encoding='utf-8-sig') as f: 
 -             f.write(year + ',' + name + ',' + team + ',' + salary + 'n') 
 -  
 -  
 - def get_index(): 
 -     url_1 = 'http://www.stat-nba.com/playerList.php' 
 -     response_1 = requests.get(url=url_1, headers=headers) 
 -     html_1 = response_1.text 
 -     # 对请求结果进行编码解码处理,避免出现乱码 
 -     html_1 = html_1.encode('ISO-8859-1') 
 -     html_1 = html_1.decode('utf-8') 
 -     soup_1 = BeautifulSoup(html_1, 'html.parser') 
 -     div = soup_1.find_all(class_='playerList')[1] 
 -     for i in div.find_all(class_='name'): 
 -         player_id = i.find('a')['href'][1:] 
 -         try: 
 -             get_player(player_id) 
 -         except: 
 -             pass 
 -  
 -  
 - if __name__ == '__main__': 
 -     get_index() 
 
  
最后成功获取数据。 
                                                (编辑:52站长网) 
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! 
                     |