使用PHP从设置标签ID,类或其他网站中获取标签 [英] Use PHP to get tags within a set tag id, class or other from a Website
问题描述
示例: 示例: 我没有任何代码示例,因为我尝试了大量搜索中的众多选项,并且都返回了错误结果。 您能否请帮忙? 更新:我在这里找到了答案: 感谢所有帮助。 如果您想了解JavaScript和jQuery: 假设这个HTML: 使用此JavaScript代码(使用jQuery)(我试图简化它,但您会如果你学得好的话,做得更好): 然后在你的PHP http://www.myapp.com/mypage.php : 好吧,我不确定这是您的特例的最佳答案,但无论如何,您真的应该学习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:
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:
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 : Use this JavaScript code (with jQuery) (I'm trying to make it simple but you'll do better if you learn well) : Then in your PHP "http://www.myapp.com/mypage.php" : Well, I'm not sure this is the best answer for your special case, but anyway you really should learn JavaScript =) 这篇关于使用PHP从设置标签ID,类或其他网站中获取标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
< div id =its attributes>其文本内容< / div>< / code>< / p>他们的ID或类使用PHP的DOMDocument类,我只是无法让我的头来告诉PHP限制返回到该预定义标记。
< div id =predefined> ...返回所有此...< / div>
PHP函数来抓取< DIV>中的所有链接。在远程站点使用scrape方法
< div id =predefined> ...返回所有这些...< / div>
//从单个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
$ id = $ _POST [id];
$ content = $ _POST [content];
//注意:始终清理发布的数据,以防止注入和其他恶意代码。
//在这里你可以将数据保存到你的MySQL数据库
echoit worked!; //这是你的成功函数在你的Javascript代码中得到的回应
?>
<div id="its attributes">its text content</div>
<div id="predefined">... return all this ...</div>
<div id="predefined">... return all this ...</div>
// 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);
}
});
<?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
?>