如何根据 google chrome 检查员提供的信息发出 xml http post 请求

how to make an xml http post request from the information provided by the google chrome inspector

这个问题没有重复,因为将 user-agent 添加到 header 并不能解决任何问题。

我一直在尝试从这个 URL. It's an XML feed, not an HTML file. It's a live feed updated every second from cashpoint.com's live soccer page 获得响应。我可以很好地从 last-mentioned 页面获取 HTML 页面,但是从第一个提到的 URL 我无法检索 XML 数据。我可以用 google chrome inspector 检查它并看到响应就好了。但是它 returns b''。尝试了 get 和 post。

编辑: 尝试添加更多 headers 但仍然无效。

如果检查员可以看到它是否可以检索此信息?

下面是我的代码和一些图片(如果你太忙没时间查看链接)。

import requests

class GetFeed():

    def __init__(self):
        pass

    def live_odds(self):

        live_index_page = 'https://www.cashpoint.dk/en/live/index.html'
        live_oddsupdate = 'https://www.cashpoint.dk/index.php?r=games/oddsupdate'

        r = requests.get(live_oddsupdate)

        print(r.text)

feed = GetFeed()
feed.live_odds()

好吧,首先,在 Chrome 控制台中,您可以看到它是一个 POST 请求,并且您似乎在 Python 代码中执行 GET 请求。

您需要在 post 请求中包含一些数据和一些 headers。试试这个:

url = 'https://www.cashpoint.dk/index.php?r=games/oddsupdate'

headers = {
    "X-Requested-With": "XMLHttpRequest",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
    "Content-Type": "application/x-www-form-urlencoded",
    "Cookie": "_ga=GA1.2.517291307.1531264976; _gid=GA1.2.1421702183.1531264976; _pk_id.155.9810=7984b0a0e139caba.1531264979.1.1531264979.1531264979.; cookieConsent=1; cpLanguage=en; langid=2; ad_network=DIRECT; PHPSESSID=f4mbbfd8adb3m59gfc1pelo126"
}

data = "parameters%5Baction%5D=odds_update&parameters%5Bgame_category%5D=live&parameters%5Bsport_id%5D=&parameters%5Btimestamp%5D=1531268162&parameters%5Bgameids%5D=%5B905814%2C905813%2C905815%2C905818%2C905792%5D&formToken=c3fed3ea6b46dae171a6f1a6d21db14fcc21474c"

response = requests.post(url, data=data, headers=headers)

print response.content

刚刚测试了这个并且它有效。这里的要点是,所有这些信息都可以在 google chrome 中完全相同的 xhr 网络检查中找到。下次,请在 post 提问之前阅读 xmlhttprequests。