如何使用PHP Simple HTML DOM Parser提取页面title和元description数据?
我只需要页面的标题和纯文本中的关键字.
我有这个 SimpleXMLElement 对象,其 XML 设置类似于以下内容...
$xml = <<<EOX
<books>
<book>
<name>ABCD</name>
</book>
</books>
EOX;
$sx = new SimpleXMLElement( $xml );
Run Code Online (Sandbox Code Playgroud)
现在我有一个名为 Book 的类,其中包含信息。关于每本书。同班还可以吐出书籍信息。类似于上面的 XML 格式(嵌套块).. 例如,
$book = new Book( 'EFGH' );
$book->genXML();
... will generate
<book>
<name>EFGH</name>
</book>
Run Code Online (Sandbox Code Playgroud)
现在我试图找出一种方法,通过它我可以使用这个生成的 XML 块并将其附加为 的子项,以便现在它看起来像......例如......
// Non-existent member method. For illustration purposes only.
$sx->addXMLChild( $book->genXML() );
...XML tree now looks like:
<books>
<book>
<name>ABCD</name>
</book>
<book>
<name>EFGH</name>
</book>
</books>
Run Code Online (Sandbox Code Playgroud)
根据我在 SimpleXMLElement 上阅读的文档,addChild()不会为您完成此操作,因为它不支持 XML 数据作为标记值。
我正在抓取(使用 PHP 简单 HTML DOM)许多不同的(新闻)网站,目的是获取页面上的主要内容/文本正文。
要做到这一点,我能想到的最好方法是找到主标题/标题 (H1) 并获取与此标题标记相同的 div 中包含的文本。
在下面的两个示例中,我将如何获取整个(父级?)div 的内容。
<div> <----- need to get contents of this whole div (containing the h1 and likely the main body of text)
<h1></h1>
main body of text here
</div>
Run Code Online (Sandbox Code Playgroud)
Div 可能在树的更上面。
<div> <----- need to get contents of this whole div
<div>
<h1></h1>
</div>
<div>
main body of text here
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
在树上进一步划分。
<div> <----- need to get contents of this whole div
<div>
<div>
<h1></h1>
</div>
<div>
main body …Run Code Online (Sandbox Code Playgroud) 我使用简单的html dom来解析itunes预览页面
当我把URL放在代码[download] url [/ download]一旦一切都好,但当我有2个或更多[download] url [/ download]代码我收到错误
致命错误:无法在第80行的/var/.../simple_html_dom.php中重新声明file_get_html()(之前在/var/.../simple_html_dom.php:65中声明)
function download_link($link){
$file=ROOT_DIR."/engine/data/obmennik.php";
include '/var/.../simple_html_dom.php'; //???????
require($file);
$site=parse_url($link);
$domain=$site['host'];
$itunes = file_get_html($link); //????
$image = $itunes->find('img.artwork[width=175]',0); //????
$e = $itunes->find("div.price", 0);
$u = $itunes->find("h1", 0);
$star = $itunes->find("div.rating", 0);
$un = $itunes->find("div.fat-binary-blurb", 0);
$dt=date('Y-m-d [H:00]');
if(array_key_exists($domain,$link_arr)){
$link="\n".'<br><a title="??????? ???? c '.$domain.'" rel="nofollow" target="_blank" href="http://li.ru/go?'.$link.'"><div style="-webkit-border-radius: 8px;-moz-border-radius: 8px;border-radius: 8px;background: #DCDCDC;"><table><tr><td width="35%"><img class="its_small" src=\"'.$image->src.'\" /></td><td width="65%" valign="middle" align="center"><span style="font-size:16px; font-weight: bold;"> '.iconv("UTF-8","cp1251",$u->plaintext). '<br> [iTunes Link, '.iconv("UTF-8","cp1251",$e->plaintext).']</span><br><br>'.iconv("UTF-8","cp1251",$un->outertext).'<br>??????? ??: '.$dt.' '.$star->outertext.'</td></tr></table></div></a>';
} return $link; …Run Code Online (Sandbox Code Playgroud)