AJAX 解析 +雅虎 YQL 没有返回结果?

发布于 2024-12-26 04:42:24 字数 1740 浏览 2 评论 0原文

我正在编写一个脚本,通过雅虎的 YQL 从外部网站获取所有 元素。这最近工作得很好,但从今天开始就停止工作了。我不完全确定为什么,所有网站都使用此代码:

<script type="text/javascript">
$(document).ready(function () {
    var container = $('#target');
    function doAjax(url) {
        if (url.match('^http')) {
            $.getJSON("http://query.yahooapis.com/v1/public/yql?" 
            + "q=select%20*%20from%20html%20where%20url%3D%22" 
            + encodeURIComponent(url) 
            + "%22&format=xml'&callback=?", 
            function (data) {
                if (data.results[0]) {
                    var fullResponse = $(filterData(data.results[0])),
                        justTable = fullResponse.find("body");
                    container.append(justTable);
                } else {
                    var errormsg = '<p>Error: could not load the page.</p>';
                    container.html(errormsg);
                }
            });
        } else {
            $('#target').load(url);
        }
    }
    function filterData(data) {
        data = data.replace(/<?\/body[^>]*>/g, '');
        data = data.replace(/[\r|\n]+/g, '');
        data = data.replace(/<--[\S\s]*?-->/g, '');
        data = data.replace(/<noscript[^>]*>[\S\s]*?<\/noscript>/g, '');
        data = data.replace(/<script[^>]*>[\S\s]*?<\/script>/g, '');
        data = data.replace(/<script.*\/>/, '');
        data = data.replace(/<img[^>]*>/g, '');
        return data;
    }
    doAjax('http://www.google.com');
});
</script>

我将 url 更改为 google 并将其更改为查找 标记而不是 ; 标签以更好地显示其不起作用。我查看了它请求的 URL,但没有显示任何内容。但不确定问题是什么。

I'm working on a script that gets all the <table> elements from an external website by going through Yahoo's YQL. This has worked fine recently, but it stopped working as of today. I'm not entirely sure why, all websites used to work with this code:

<script type="text/javascript">
$(document).ready(function () {
    var container = $('#target');
    function doAjax(url) {
        if (url.match('^http')) {
            $.getJSON("http://query.yahooapis.com/v1/public/yql?" 
            + "q=select%20*%20from%20html%20where%20url%3D%22" 
            + encodeURIComponent(url) 
            + "%22&format=xml'&callback=?", 
            function (data) {
                if (data.results[0]) {
                    var fullResponse = $(filterData(data.results[0])),
                        justTable = fullResponse.find("body");
                    container.append(justTable);
                } else {
                    var errormsg = '<p>Error: could not load the page.</p>';
                    container.html(errormsg);
                }
            });
        } else {
            $('#target').load(url);
        }
    }
    function filterData(data) {
        data = data.replace(/<?\/body[^>]*>/g, '');
        data = data.replace(/[\r|\n]+/g, '');
        data = data.replace(/<--[\S\s]*?-->/g, '');
        data = data.replace(/<noscript[^>]*>[\S\s]*?<\/noscript>/g, '');
        data = data.replace(/<script[^>]*>[\S\s]*?<\/script>/g, '');
        data = data.replace(/<script.*\/>/, '');
        data = data.replace(/<img[^>]*>/g, '');
        return data;
    }
    doAjax('http://www.google.com');
});
</script>

I changed the url to google and changed it to find the <body> tag instead of <table> tags to better show its not working. I looked at the URL that it's requesting and it's not showing any content. Not sure what the problem is though.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南风几经秋 2025-01-02 04:42:24

您是否检查过您抓取的“外部网站”是否发生了结构变化?
当它以前有效但现在不再有效时,我的提示是网站结构已经改变。

Have you checked if the "external website" you have crawled has structural changes?
When it has worked before and now not anymore, then my tip is that the site structure has changed.

南城追梦 2025-01-02 04:42:24

看来问题是YQL挂了?我刚刚再次测试了一下,效果很好。我希望他们将来如果发生停电时能告诉我们。

It looks like the problem was that YQL was down? I just tested it again and it worked out fine. I wish they would tell us in the future if an outage occurred.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文