卡住了。我试图提取所有的html标签,他们的属性和他们的文本内容是在一个预定义的标签从远程网站。
的例子:<div id="its attributes">its text content</div>
我可以通过使用php的DOMDocument类使用它们的id或类提取任何标签,我只是不能让我的头告诉php限制返回到预定义的标签。
的例子:<div id="predefined">... return all this ...</div>
我没有任何代码的例子,因为我已经尝试了无数的选项从无数的搜索和所有返回错误的结果。
你能帮帮我吗?
更新:我在这里找到了答案:PHP函数,用于获取
远程站点使用刮擦法
Thanks to all that help .
您可以使用getElementById
,然后访问nodeValue
:
$doc->loadHTML('<html><body><div id="predefined">... return all this ...</div></body></html>');
$i = $doc->getElementById('predefined');
echo $i->nodeValue;
如果你想学习JavaScript和jQuery:
假设这个HTML:
<div id="predefined">... return all this ...</div>
使用这个JavaScript代码(jQuery)(我试图使它简单,但你会做得更好,如果你学得好):
// 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 "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
?>
好吧,我不确定这是你的特殊情况的最佳答案,但无论如何你真的应该学习JavaScript =)