如何使用curl打印特定标签
how to print text from <p>
tag using curl
and DOMDocument
i try like this but not showing anything.
<?php
$url = "http://www.blablabla.org/dorama/201105455/kimi-wa-petto";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$html = curl_exec($ch);
curl_close($ch);
$dom = new DOMDocument();
@$dom->loadHTML($html);
foreach($dom->getElementsByTagName('p') as $link) {
echo $link->getAttribute('p');
echo "<br />";
}
?>
如何使用 curl从
&lt; p&gt; code>标签打印文本 代码>和
DOMDocument code>我尝试这样但没有显示任何内容。 p>
&lt;?php
$ url =“http:// www.blablabla.org/dorama/201105455/kimi-wa-petto";
$ch = curl_init();
$ timeout = 5;
curl_setopt($ ch,CURLOPT_URL,$ url);
curl_setopt($ ch ,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ ch,CURLOPT_CONNECTTIMEOUT,$ timeout);
$ html = curl_exec($ ch);
curl_close($ ch);
$ dom = new DOMDocument();
@ $ dom-&gt; loadHTML($ html);
foreach($ dom-&gt; getElementsByTagName('p')as $ link){
echo $ link-&gt; getAttribute('p');
echo“&lt; br /&gt;”;
}
?&gt;
code> pre>
div>
If you just want to print out the text inside the <p>
tags, the use $textContent
...
foreach($dom->getElementsByTagName('p') as $link) {
echo $link->textContent;
echo "<br />";
}
If you want the whole XML, you can output it using saveXML()
with the node as the point to output from ...
foreach($dom->getElementsByTagName('p') as $link) {
echo $dom->saveXML( $link);
echo "<br />";
}
As this is HTML, you may want to replace the saveXML()
call with saveHTML()
.