Python自动刷新ajax?

2024-04-29 08:26:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我刮了cricbuzz网站查看分数和其他细节..它将显示分数与一些细节每30秒…如何自动更新,如果分数在cricbuzz网站通过ajax的变化。。。有模块吗?你知道吗

    from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
import time
while True:


    html = urlopen("http://www.cricbuzz.com/live-cricket-scores/16478/aus-vs-nz-1st-odi-new-zealand-tour-of-australia-2016")
    bsObj= BeautifulSoup(html,"html5lib")
#print(bsObj.prettify())
    players=[]
    detail=[]
    scores =bsObj.find("span",{"class":"cb-font-20 text-bold"})
    print(scores.get_text())
    detail.append(scores.get_text())
    status=bsObj.find("div",{"class":{"cb-text-inprogress","cb-text-lunch","cb-text-stump","cb-text-innings break","cb-text-tea"}})
    print("Status: ")
    print(status.get_text())
    detail.append(status.get_text())
    for link in bsObj.find('div', {"class":"cb-min-inf cb-col-100"}).find("div").next_siblings:
        if link.a.get_text() not in players:
            players.append(link.a.get_text())
            players.append(link.find('div',{"class":"cb-col cb-col-10 ab text-right"}).string)
    print("Players: ")                    
    for i in players:
        print(i)
    detail.append(players)
    print(detail)

    print("-----------------------------------------------------------------------------------")
    time.sleep(30)

Tags: textimportdivgetlinkfindclasscb
1条回答
网友
1楼 · 发布于 2024-04-29 08:26:36

因此,如果您愿意从http://www.cricbuzz.com/live-cricket-scores/16478/aus-vs-nz-1st-odi-new-zealand-tour-of-australia-2016获取实时游戏信息,那么该url就不是您想要一直请求的确切页面。你知道吗

基本上如您所说,该页面是由ajax请求更新的,所以您希望遵循特定的ajax url,在本例中是http://push.cricbuzz.com/match-push?id=16478,它是一个JSON就绪的对象,这有多好?你知道吗

{
    "settings": {
        "refresh": 15000,
        "timeout": 30000,
        "pull_geo": [],
        "force_refresh": false,
        "video_geo": {
            "country": ["US",
            "CA"]
        },
        "video_enabled": false,
        "mode": "push"
    },
    "id": "16478",
    "series": {
        "id": "2444",
        "name": "New Zealand tour of Australia, 2016",
        "short_name": "NZ tour of AUS",
        "type": "",
        "tour": false,
        "start_date": "1480723200",
        "end_date": "1481328000",
        "category": "International"
    },
    "start_time": "1480821600",
    "exp_end_time": "1480850400",
    "state": "inprogress",
    "dn": true,
    "match_desc": "1st ODI",
    "type": "ODI",
    "live_coverage": true,
    "minor_series": false,
    "state_title": "Live",
    "status": "New Zealand need 257 runs",
    "score": {
        "over_summary": {
            "over": "5",
            "ball_def": "30",
            "rem_over": "5",
            "runs": "33",
            "wickets": "0",
            "fours": "6",
            "sixes": "0"
        },
        "prev_overs": " . 1 . . . . | 4 . 4 . . 4 | . . 1 1 . 4 | 4 . . 1 ",
        "batting": {
            "id": "13",
            "score": "68/2 (13.4 Ovs)",
            "innings": [{
                "id": "2",
                "score": "68",
                "wkts": "2",
                "overs": "13.4"
            }]
        },
        "bowling": {
            "id": "4",
            "score": "324/8 (50.0 Ovs)",
            "innings": [{
                "id": "1",
                "score": "324",
                "wkts": "8",
                "overs": "50"
            }]
        },
        "rrr": "7.07",
        "max_overs": "50",
        "crr": "4.98",
        "target": "325",
        "prtshp": "34(35)",
        "last_wkt": "6326",
        "last_wkt_score": "9 (16)",
        "batsman": [{
            "id": "8983",
            "strike": "1",
            "r": "12",
            "b": "15",
            "4s": "2",
            "6s": "0"
        },
        {
            "id": "1457",
            "strike": "0",
            "r": "43",
            "b": "45",
            "4s": "5",
            "6s": "2"
        }],
        "bowler": [{
            "id": "8095",
            "o": "1.4",
            "m": "0",
            "r": "17",
            "w": "0"
        },
        {
            "id": "6250",
            "o": "3",
            "m": "0",
            "r": "12",
            "w": "0"
        }]
    },
    "hys": "false",
    "toss": {
        "winner": "Australia",
        "decision": "Batting"
    },
    "team1": {
        "id": "4",
        "name": "Australia",
        "s_name": "AUS",
        "round_flag": "4_50.png",
        "square_flag": "team_4_50.png",
        "squad": [1739,
        1643,
        2250,
        1440,
        6250,
        8497,
        1649,
        8095,
        7710,
        8642,
        6258],
        "squad_bench": [7662,
        7789,
        8876]
    },
    "team2": {
        "id": "13",
        "name": "New Zealand",
        "s_name": "NZ",
        "round_flag": "13_50.png",
        "square_flag": "team_13_50.png",
        "squad": [1457,
        8216,
        6326,
        8983,
        8085,
        1459,
        10100,
        8081,
        9067,
        10692,
        8117],
        "squad_bench": [10694,
        1057,
        8676]
    },
    "comm_lines": [{
        "timestamp": "1480840812865",
        "i_id": "2",
        "evt": "other",
        "comm": "A short-leg is in place now.."
    },
    {
        "timestamp": "1480840809865",
        "i_id": "2",
        "o_no": "13.4",
        "b_no": "82",
        "score": "68",
        "wkts": "2",
        "batsman": [{
            "id": "1457",
            "strike": "1",
            "4s": "5",
            "6s": "2",
            "r": "43",
            "b": "45"
        }],
        "bowler": [{
            "id": "8095",
            "o": "1",
            "m": "0",
            "r": "17",
            "w": "0",
            "n": "0",
            "wd": "0"
        }],
        "evt": "other",
        "comm": "P Cummins to Guptill, 1 run, Cummins has been way too short in this spell, Guptill rides the bounce and cuts it square past cover, a single to the man in the deep"
    },
    {
        "timestamp": "1480840769121",
        "i_id": "2",
        "o_no": "13.3",
        "b_no": "81",
        "score": "67",
        "wkts": "2",
        "batsman": [{
            "id": "1457",
            "strike": "1",
            "4s": "5",
            "6s": "2",
            "r": "42",
            "b": "44"
        }],
        "bowler": [{
            "id": "8095",
            "o": "1",
            "m": "0",
            "r": "16",
            "w": "0",
            "n": "0",
            "wd": "0"
        }],
        "evt": "other",
        "comm": "P Cummins to Guptill, no run, short and wide from Cummins, Guptill hopped across on the ferocious cut, gets beaten on the top-edge"
    },
    {
        "timestamp": "1480840731850",
        "i_id": "2",
        "o_no": "13.2",
        "b_no": "80",
        "score": "67",
        "wkts": "2",
        "batsman": [{
            "id": "1457",
            "strike": "1",
            "4s": "5",
            "6s": "2",
            "r": "42",
            "b": "43"
        }],
        "bowler": [{
            "id": "8095",
            "o": "1",
            "m": "0",
            "r": "16",
            "w": "0",
            "n": "0",
            "wd": "0"
        }],
        "evt": "other",
        "comm": "P Cummins to Guptill, no run, Cummins responds with a bouncer, Guptill drops his wrists and lets it go"
    },
    {
        "timestamp": "1480840699390",
        "i_id": "2",
        "o_no": "13.1",
        "b_no": "79",
        "score": "67",
        "wkts": "2",
        "batsman": [{
            "id": "1457",
            "strike": "1",
            "4s": "5",
            "6s": "2",
            "r": "42",
            "b": "42"
        }],
        "bowler": [{
            "id": "8095",
            "o": "1",
            "m": "0",
            "r": "16",
            "w": "0",
            "n": "0",
            "wd": "0"
        }],
        "evt": "four",
        "all_evt": ["four"],
        "comm": "P Cummins to Guptill, <b>FOUR<\/b>, Cummins is leaking boundaries, this was full and wide of off. Guptill reaches out and slices the drive - square on the off-side, beats the fielder getting across from deep cover"
    }],
    "latest_video": {
        "timestamp": "1480839773003",
        "comm": "Hazlewood to Neesham, <b>FOUR<\/b>, nicely played by Neesham, full and on Neesham's pads, he gets inside the line and clips it past mid-wicket, the timing is sweet and the ball rolls away to the fence",
        "comm": "Hazlewood to Neesham, <b>FOUR<\/b>, nicely played by Neesham, full and on Neesham's pads, he gets inside the line and clips it past mid-wicket, the timing is sweet and the ball rolls away to the fence",
        "o_no": "9.4",
        "video": "0_qbtvgqc4"
    }
}

现在只需使用json模块来解析它并提取您想要的任何内容。你知道吗

相关问题 更多 >