首页 > 零售 > 正文

看热讯:python练习题——爬虫爬取NBA球员数据(虎扑)

2023-05-06 07:08:02来源:哔哩哔哩  


【资料图】

提示:本专栏仅方便up主自己做笔记,他人切勿用于非法商业活动。

源代码如下:

'''python爬虫获取NBA球员信息'''import requestsimport tkinter.messageboxfrom lxml import etreedef data_analysis(e):    '''解析数据'''    nums = e.xpath('//table[@class="players_table"]//tr/td[1]/text()')    names = e.xpath('//table[@class="players_table"]//tr/td[2]/a/text()')    teams = e.xpath('//table[@class="players_table"]//tr/td[3]/a/text()')    scores = e.xpath('//table[@class="players_table"]//tr/td[4]/text()')    sessions = e.xpath('//table[@class="players_table"]//tr/td[11]/text()')    sessions = sessions[2::1]    playing_times = e.xpath('//table[@class="players_table"]//tr/td[12]/text()')    playing_times = playing_times[2::1]    nba_player_data = zip(nums, names, teams, scores, sessions, playing_times)    return nba_player_datadef save_txt(nba_player_data):    '''数据存储为NBA.txt'''    with open('nba.txt', 'w', encoding='utf-8') as f:        f.write('\t数据均来自https://nba.hupu.com/stats/players\n')        for num, name, team, score, session, playing_time in nba_player_data:            f.write(f'排名:{num} 姓名:{name} 球队:{team} 得分:{score} 场次:{session} 上场时间:{playing_time}\n')    f.close()    return True#发送地址,url = 'https://nba.hupu.com/stats/players'heards = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.68'}#发送请求resp = requests.get(url, headers = heards)#print(resp.text)#处理结果e = etree.HTML(resp.text)#解析响应数据nba_player_data = data_analysis(e)#是否保存condition = save_txt(nba_player_data)if(condition == True):    tkinter.messagebox.showinfo("提示", "获取成功")else:    tkinter.messagebox.showinfo("提示", "获取失败")

关键词:

责任编辑:hnmd003

精彩推送