【问题标题】:How to read data from an API which gives Unauthorized error when accessing the Request URL如何从访问请求 URL 时出现未经授权错误的 API 读取数据
【发布时间】:2020-08-19 19:01:28
【问题描述】:

我正在尝试“抓取”一个网站,但是当我尝试打开请求 url 时,它给了我未经授权的 401 状态错误。

网址:https://www.dbxkjfejjekef.com/

请求网址:https://www.db.co.in/__api__/api/1.0/feed/home

错误信息:

{
   error: "Unauthorized",
   message: "Check failed",
   path: "/__api__/api/1.0/feed/home",
   timestamp: 1597861914142,
   status: 401
}

在开发者工具的预览部分,我可以看到数据:

如何抓取这些数据,使其看起来像:

Title : slug + title
URL : shareUri

我可以写代码,但我不知道要解析什么。

【问题讨论】:

    标签: python-3.x web-scraping beautifulsoup request


    【解决方案1】:

    要获得正确的响应,请设置x-aut-web-t HTTP 标头:

    import json
    import requests
    
    
    url = 'https://www.divyabhaskar.co.in/__api__/api/1.0/feed/home'
    headers = {
        'x-aut-web-t': '420x66695ztde3qao6a69'
    }
    
    data = requests.get(url, headers=headers).json()
    print(json.dumps(data, indent=4))
    

    打印:

    {
        "metaTitle": "Gujarati News, News in Gujarati \u2013 \u0a97\u0ac1\u0a9c\u0ab0\u0abe\u0aa4 \u0ab8\u0aae\u0abe\u0a9a\u0abe\u0ab0 | \u0aa6\u0abf\u0ab5\u0acd\u0aaf \u0aad\u0abe\u0ab8\u0acd\u0a95\u0ab0",
        "metaDescription": "Gujarati News Samachar - Find all Gujarati News and Samachar, News in Gujarati, Gujarat News, Gujarati News Headlines and Daily Breaking News, Gujarati News Paper in DivyaBhaskar.co.in.",
        "metaKeywords": "News, Gujarati news, news in Gujarati, newspaper, Gujarati newspaper, online news, daily news, national news, India news, political news, sports news",
        "cursor": "eyJ2IjoxLCJoYXNwIjp0cnVlLCJmbWF0IjpudWxsLCJsbWF0IjoxNTk3ODQwNTc0MDAwfQ",
        "feed": [
            {
                "storyId": 127630068,
                "shareUri": "https://divya-b.in/CQ4aHWCa48",
                "priority": 1,
    
    ...and so on.
    

    【讨论】:

    • 谢谢安德烈。它按预期工作,但我在打印时看到奇怪的字符: "title": "\u0a9b\u0ac7\u0ab2\u0acd\u0ab2\u0abe 24 \u0a95\u0ab2\u0abe\u0a95\u0aae\u0abe\u0a82 63 \u0ab9\u0a9c\ u0abe\u0ab0\u0aa5\u0ac0 \u0ab5\u0aa7\u0ac1 \u0a9f\u0ac7\u0ab8\u0acd\u0a9f, 1,145 \u0aa8\u0ab5\u0abe \u0aa6\u0ab0\u0acd\u0aa6\u0ac0 \u0a85\u0aa8\u0ac7a8 \u0abe \u0aae\u0acb\u0aa4, \u0a95\u0ac1\u0ab2 \u0a95\u0ac7\u0ab8 82 \u0ab9\u0a9c\u0abe\u0ab0\u0aa8\u0ac7 \u0aaa\u0abe\u0ab0",
    • @parikhparth23 这并不奇怪,它是 unicode 表示 - 尝试使用 print(data['metaTitle']),它应该打印正确的 unicode 字符。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-06
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 2022-11-09
    • 1970-01-01
    相关资源
    最近更新 更多