【问题标题】:How to correctly parse XML urls with requests in Python?如何使用 Python 中的请求正确解析 XML url?
【发布时间】:2020-04-16 23:19:54
【问题描述】:

我想从一个 URL 解析一个 XML 文件。

通过执行以下操作:

req = requests.get('https://www.forbes.com/news_sitemap.xml')

我没有得到正确的 XML 文件,而是得到:

<!doctype html>
<html lang="en">
        <head>
                <meta http-equiv="Content-Language" content="en_US">

                <script type="text/javascript">
                        (function () {
                                function isValidUrl(toURL) {
                                        // Regex taken from welcome ad.
                                        return (toURL || '').match(/^(?:https?:?\/\/)?(?:[^.(){}\\\/]*)?\.?forbes\.com(?:\/|\?|$)/i);
                                }

                                function getUrlParameter(name) {
                                        name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
                                        var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
                                        var results = regex.exec(location.search);
                                        return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
                                };

                                function consentIsSet(message) {
                                        console.log(message);
                                        var result = JSON.parse(message.data);
                                        if(result.message == "submit_preferences"){
                                                var toURL = getUrlParameter("toURL");
                                                if(!isValidUrl(toURL)){
                                                        toURL = "https://www.forbes.com/";
                                                }
                                                location.href=toURL;
                                        }
                                }

                                var apiObject = {
                                        PrivacyManagerAPI:
                                        {
                                                action: "getConsent",
                                                timestamp: new Date().getTime(),
                                                self: "forbes.com"
                                        }
                                };
                                var json = JSON.stringify(apiObject);
                                window.top.postMessage(json,"*");
                                window.addEventListener("message", consentIsSet, false);
                        })();
                </script>
        </head>
        <div id='teconsent'>
                <script async="async" type="text/javascript" crossorigin src='//consent.truste.com/notice?domain=forbes.com&c=teconsent'></script>
        </div>
        <body>
        </body>
</html>

是否还有更好的方法来处理 XML 文件(例如,如果它被压缩,或者如果文件太大,则通过递归解析它......)?谢谢!

【问题讨论】:

    标签: python xml python-requests


    【解决方案1】:

    如果您提供该 cookie 以请求您可以获得 XML 文件,本网站会检查该 cookie 是否符合 GDPR。 试试这个代码,对我来说很好。

    import requests
    url = "https://www.forbes.com/news_sitemap.xml"
    news_sitemap = requests.get(url, headers={"Cookie": "notice_gdpr_prefs=0,1,2:1a8b5228dd7ff0717196863a5d28ce6c"})
    
    print(news_sitemap.text)
    

    【讨论】:

      【解决方案2】:

      使用 requests 模块我得到了 xml 文件。然后,您可以使用 xml 解析器库来做您想做的事情。

      import requests
      url = "https://www.forbes.com/news_sitemap.xml"
      x = requests.get(url)
      print(x.text)
      

      【讨论】:

        猜你喜欢
        • 2014-01-15
        • 1970-01-01
        • 2011-05-11
        • 2023-03-11
        • 1970-01-01
        • 1970-01-01
        • 2021-09-14
        • 2020-04-18
        • 2017-06-01
        相关资源
        最近更新 更多