【问题标题】:How to Extract a specific div content from a external website? [duplicate]如何从外部网站提取特定的 div 内容? [复制]
【发布时间】:2023-03-05 23:44:01
【问题描述】:

我正在尝试使用 Jquery .load() 方法从外部网站提取一些信息,但这似乎不起作用,有什么想法吗?我在 Firefox 和 chrome 上都进行了测试。

 <!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("#div1").load("http://www.weather.com/weather/5-day/CEXX0001 #wx-forecast-container");
  });
});
</script>
</head>
<body>

<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>
<button>Get External Content</button>

</body>
</html>

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    它会抛出错误
    请求的资源上不存在“Access-Control-Allow-Origin”标头

    原因是您无法通过 XmlHttpRequest 访问外部域 url。

    另一种方法是创建服务器端中间程序,从 weather.com 提取数据并使用 load 或 ajax 方法提供给您的页面。

    【讨论】:

      【解决方案2】:

      您正面临跨域政策限制。浏览器根本不允许您从外部网站加载内容, 您可以尝试将 crossdomain.xml 放在 weather.com 上,但不能

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-10-16
        • 2013-04-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-24
        • 1970-01-01
        相关资源
        最近更新 更多