【效率工具】基于python爬虫的“nba对阵信息及数据统计信息查看“Alfred小工具

1.背景

每次都需要打开网页看nba消息,太繁琐了,利用mac端的效率神器Alfred,编写了一个python脚本爬取新浪nba的消息,敲一个命令就能展示出当天的nba对阵信息,回车一下直接跳转到数据统计的网页。

2.具体操作

alfred-nba

https://www.bilibili.com/video/BV1MC4y187tJ/

3.核心逻辑

from urllib import request, parse
import sys
import json
import copy
import time

def getNBA(  ):
    url = 'https://slamdunk.sports.sina.cn/api?p=radar&s=schedule&a=date_limit&order=-1&date=&_=' + str(time.time()*1000)

    response = getJsonFromUrl(url)  

    items = {"items": []}
    template = {
        "title": "",
        "subtitle": "",
        "arg": ""
    }

    for i in response['result']['data']['matchs']:
        url = 'https://slamdunk.sports.sina.cn/api?p=radar&s=boxscore&a=match&dpc=1&mid=' + i['mid']
        response1 = getJsonFromUrl( url )
        template["title"] = i['home_name'] + 'vs' + i['away_name'] + '=>' + str(i['home_score']) + ':' + str(i['away_score'])
        template["subtitle"] = '开始时间:' + i['date'] + '    ' + i['time'] + '  比赛状态:' + response1['result']['data']['time_str']
        template["arg"] = 'https://slamdunk.sports.sina.cn/nano/game?livetype=nba&matchid=' + response1['result']['data']['cmt_newsid'].split('-',)[2]
        items["items"].append(copy.deepcopy(template));
    jsonstr = json.dumps(items)
    print(jsonstr)  # 打印字符串就可以被alfred识别

def getJsonFromUrl ( url ):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/79.0.3945.88 Safari/537.36',
    }
    req = request.Request(url=url, method='GET', headers=headers)
    response = json.loads(request.urlopen(req).read().decode('utf-8')) 
    return response

if __name__ == '__main__':
    getNBA()

 

你可能感兴趣的:(测试工程师)