【问题标题】:How to extract XHR response data from the a website?如何从网站中提取 XHR 响应数据?
【发布时间】:2019-10-06 05:19:53
【问题描述】:

我想获得一些网页加载后下载的一种 json 文档的链接。例如this webpage

但它可能是a different webpage 上的一个非常不同的文档。 不幸的是,我在 Beautfiul 汤的源页面中找不到链接。

到目前为止我试过这个:

import requests
import json

data = {
  "Device[udid]": "",
  "API_KEY": "",
  "API_SECRET": "",
  "Device[change]": "",
  "fbToken": ""
}

headers = {
  "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36"
}

url = "https://data.electionsportal.ge/en/event_type/1/event/38/shape/69898/shape_type/1?data_type=official"

r = requests.post(url, data=data, headers=headers)
data = r.json()

但它返回一个 json 解码错误:

---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-72-189954289109> in <module>
     17 
     18 r = requests.post(url, data=data, headers=headers)
---> 19 data = r.json()
     20 

C:\ProgramData\Anaconda3\lib\site-packages\requests\models.py in json(self, **kwargs)
    895                     # used.
    896                     pass
--> 897         return complexjson.loads(self.text, **kwargs)
    898 
    899     @property

C:\ProgramData\Anaconda3\lib\json\__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    346             parse_int is None and parse_float is None and
    347             parse_constant is None and object_pairs_hook is None and not kw):
--> 348         return _default_decoder.decode(s)
    349     if cls is None:
    350         cls = JSONDecoder

C:\ProgramData\Anaconda3\lib\json\decoder.py in decode(self, s, _w)
    335 
    336         """
--> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338         end = _w(s, end).end()
    339         if end != len(s):

C:\ProgramData\Anaconda3\lib\json\decoder.py in raw_decode(self, s, idx)
    353             obj, end = self.scan_once(s, idx)
    354         except StopIteration as err:
--> 355             raise JSONDecodeError("Expecting value", s, err.value) from None
    356         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

【问题讨论】:

    标签: python python-3.x request xmlhttprequest


    【解决方案1】:

    您试图在 HTML 内容中找到的JSON 是由客户端通过带有 XMLHttpRequests 的 javascript 加载的。这意味着您将无法使用 BeautifulSoup 在包含 URL 的 HTML 中查找标签,它位于 &lt;script&gt; 块中或外部加载。

    此外,您正在尝试将用 HTML 编写的网页转换为 JSON。并尝试访问网页或 JSON 内容中未定义的密钥(coins)。

    解决方案

    1. 直接加载该 JSON,而无需尝试在上述网站中使用 BeautifulSoup 查找 JSON URL。这样,您就可以完美地运行requests.json()

    2. 否则,请查看Selenium,它是一个允许您运行 javascript 的网络驱动程序。

    希望能解决这个问题。

    【讨论】:

    • 是的,对coins 感到抱歉,这是一个不应该留在那里的剩余测试。如何使用 Python 自动加载该 json?我想用一个脚本来做,因为我想在几个页面上做。
    【解决方案2】:

    这适用于您帖子中的两个链接:

    from bs4 import BeautifulSoup
    import requests
    url = 'https://data.electionsportal.ge/en/event_type/1/event/38/shape/69898/shape_type/1?data_type=official'
    r = requests.get(url)
    soup = BeautifulSoup(r.text)
    splits = [item.split('=',1)[-1] for item in str(soup.script).split(';')]
    filtered_splits = [item.replace('"','') for item in splits if 'json' in item and not 'xxx' in item]
    links_to_jsons = ["https://data.electionsportal.ge" + item for item in    filtered_splits]
    for item in links_to_jsons:
       r = requests.get(item)
       print(r.json())       # change as you want      
    

    顺便说一句。我猜您可以通过将数字 69898 更改为在另一个网页中处于相似位置的数字来构建 json 链接(但仍然是 data.electionsportal.ge)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-03
      相关资源
      最近更新 更多