简体   繁体   English

使用javascript获取外部内容

[英]Get external content using javascript

I would like make a website which will go to an external web page and get a content that is between some div. 我想制作一个网站,该网站可以转到外部网页并获取介于某些div之间的内容。

External content url: http://www.fazenda.org.br/palavra_vida/ 外部内容网址: http//www.fazenda.org.br/palavra_vida/

Content code that I want to extract: 我要提取的内容代码:

<div class="bloco">
  <h2>Palavra do Dia</h2>
  <span>
  <p><span class="negrito">Todo aquele que é da verdade escuta a minha voz</span>&nbsp;Jo 18, 33b-37</p><p>Jesus Cristo, Rei do Universo </p>      </span> </div>

How can I get the content that is between 我如何获得介于两者之间的内容

<p> <span class="negrito"> Todo aquele que é da verdade escuta a minha voz</span>&nbsp;Jo 18, 33b-37</p><p>Jesus Cristo, Rei do Universo </p>      </span>

Thank you all! 谢谢你们!

In this case it would not be possible since you would need to perform an AJAX request and since the destination it is considered a security risk and would be blocked by the web browser with an error message similar to this one: 在这种情况下,这将是不可能的,因为您将需要执行AJAX请求,并且由于目的地将其视为安全隐患,并且将被Web浏览器阻止,并显示类似于以下内容的错误消息:

XMLHttpRequest cannot load http://www.fazenda.org.br/palavra_vida/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://yourdomain.com' is therefore not allowed access.

You would need to do it via whatever server-side language you are using. 您将需要通过所使用的任何服务器端语言来实现。

Just to show an example of how could you do it if the destination page belonged to the same domain: 仅显示一个示例,说明如果目标页面属于同一域,您将如何执行此操作:

$.ajax({
     url: "/palavra_vida/",
     dataType: 'text',
     success: function(data) {
          var phrase = $(data).find("div.bloco > span > p > span.negrito").text();
          console.log(phrase);
     }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM