【问题标题】:Python automatic ajax refresh?Python自动ajax刷新?
【发布时间】:2017-04-18 19:19:35
【问题描述】:

我爬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)

【问题讨论】:

    标签: python ajax refresh


    【解决方案1】:

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

    基本上如您所说,该页面是由 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 模块来解析它并提取你想要的任何东西。

    【讨论】:

      猜你喜欢
      • 2011-03-24
      • 2015-02-08
      • 2013-09-01
      • 2017-12-14
      • 2012-06-19
      • 2012-12-08
      • 2016-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多