使用PHP从设置标签ID,类或其他网站中获取标签 [英] Use PHP to get tags within a set tag id, class or other from a Website

查看:131
本文介绍了使用PHP从设置标签ID,类或其他网站中获取标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我卡住了。我试图从远程网站中提取所有html标签,它们的属性以及它们的文本内容,这些标签都位于预定义标签内。



示例:
< div id =its attributes>其文本内容< / div>< / code>< / p>他们的ID或类使用PHP的DOMDocument类,我只是无法让我的头来告诉PHP限制返回到该预定义标记。

示例:
< div id =predefined> ...返回所有此...< / div>



我没有任何代码示例,因为我尝试了大量搜索中的众多选项,并且都返回了错误结果。

您能否请帮忙?

更新:我在这里找到了答案:
PHP函数来抓取< DIV>中的所有链接。在远程站点使用scrape方法



感谢所有帮助。 如果您想了解JavaScript和jQuery:



假设这个HTML:

 < div id =predefined> ...返回所有这些...< / div> 

使用此JavaScript代码(使用jQuery)(我试图简化它,但您会如果你学得好的话,做得更好):

  //从单个HTML标签获取内容
var theID = 预定的;
var theContent = $(#+ theID).text(); //或.html()如果你想要整个内容

//将提取的内容发布到你的PHP页面以写入你的mySQL数据库
$ .ajax({
type:POST,
url:http://www.myapp.com/mypage.php,
data:{id:theID,content:theContent},
成功:function(response){//以防万一您需要PHP东西的响应
alert(response);
}
});

然后在你的PHP http://www.myapp.com/mypage.php

 <?php 
$ id = $ _POST [id];
$ content = $ _POST [content];

//注意:始终清理发布的数据,以防止注入和其他恶意代码。

//在这里你可以将数据保存到你的MySQL数据库

echoit worked!; //这是你的成功函数在你的Javascript代码中得到的回应
?>

好吧,我不确定这是您的特例的最佳答案,但无论如何,您真的应该学习JavaScript =)

I'm stuck. I'm trying to extract all html tags, their attributes and their text content that are inside a predefined tag from a remote website.

Example: <div id="its attributes">its text content</div>

I can extract any tag using their id or class by using php's DOMDocument class, I just can't get my head around telling php to limit the return to that predefined tag.

Example: <div id="predefined">... return all this ...</div>

I don't have any examples of code, As i've tried numerous options from numerous searches and all have return false results.

Can you please help?

UPDATE: I've found the answer here: PHP function to grab all links inside a <DIV> on remote site using scrape method

Thanks to all that helped.

解决方案

Just if you want to learn about JavaScript and jQuery :

Assuming this HTML :

<div id="predefined">... return all this ...</div>

Use this JavaScript code (with jQuery) (I'm trying to make it simple but you'll do better if you learn well) :

// To get the content from a single HTML tag
var theID = "predefined";
var theContent = $("#" + theID).text(); // Or .html() if you want the whole content

// To POST the extracted content to your PHP page in order to write to your mySQL db
$.ajax({
    type: "POST",
    url: "http://www.myapp.com/mypage.php",
    data: { id: theID, content: theContent },
    success: function(response){ // just in case you need a response from your PHP stuff
        alert(response);
    }
});

Then in your PHP "http://www.myapp.com/mypage.php" :

<?php
    $id = $_POST["id"];
    $content = $_POST["content"];

    // Note: Always sanitize posted data, to prevent injections and other malicious code.

    // Here you can save the data to your MySQL db

    echo "it worked!"; // This is the response that your "success" function will get in your Javascript code
?>

Well, I'm not sure this is the best answer for your special case, but anyway you really should learn JavaScript =)

这篇关于使用PHP从设置标签ID,类或其他网站中获取标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆