如何从外部网站中提取特定的div内容? [重复]

时间:2022-11-29 22:48:38

This question already has an answer here:

这个问题在这里已有答案:

I'm trying to extract some information from a external website with Jquery .load() method, But this doesn't seem to be working, any thoughts why? I tested on both firefox and chrome.

我试图用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>

2 个解决方案

#1


9  

It will throw an error
No 'Access-Control-Allow-Origin' header is present on the requested resource

它将引发错误请求的资源上没有“Access-Control-Allow-Origin”标头

Reason is you can not access external domain url through XmlHttpRequest.

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

Alternative is create server side intermediater that pull data from weather.com and serve to your page using load or ajax method.

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

#2


1  

You are facing crossdomain policy restrictions. Browser simply won't allow you to load content from external website, You can try to place crossdomain.xml on weather.com, however you can't

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

#1


9  

It will throw an error
No 'Access-Control-Allow-Origin' header is present on the requested resource

它将引发错误请求的资源上没有“Access-Control-Allow-Origin”标头

Reason is you can not access external domain url through XmlHttpRequest.

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

Alternative is create server side intermediater that pull data from weather.com and serve to your page using load or ajax method.

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

#2


1  

You are facing crossdomain policy restrictions. Browser simply won't allow you to load content from external website, You can try to place crossdomain.xml on weather.com, however you can't

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