我想创建一个网站,该网站将转到外部网页并获取介于某个div之间的内容。
外部内容网址:http://www.fazenda.org.br/palavra_vida/
我要提取的内容代码:
//playToPause()
@IBAction func playButton(sender: UIBarButtonItem) {
let newBarButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "pauseButton:")
navigationBar.topItem?.rightBarButtonItem = newBarButton
}
// pauseToPlay()
@IBAction func pauseButton(sender: UIBarButtonItem){
let pauseBtnItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "playButton:")
navigationBar.topItem!.rightBarButtonItem = pauseBtnItem
}
如何获取
之间的内容<div class="bloco">
<h2>Palavra do Dia</h2>
<span>
<p><span class="negrito">Todo aquele que é da verdade escuta a minha voz</span> Jo 18, 33b-37</p><p>Jesus Cristo, Rei do Universo </p> </span> </div>
谢谢大家!
答案 0 :(得分:0)
在这种情况下,由于您需要执行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.
您需要通过正在使用的任何服务器端语言来执行此操作。
只是为了展示一个示例,如果目标网页属于同一个域,你怎么能这样做:
$.ajax({
url: "/palavra_vida/",
dataType: 'text',
success: function(data) {
var phrase = $(data).find("div.bloco > span > p > span.negrito").text();
console.log(phrase);
}
});