【问题标题】:get content from a website by using a css-path [closed]使用 css-path 从网站获取内容 [关闭]
【发布时间】:2014-07-31 04:20:02
【问题描述】:

是否可以使用 css 路径从网站获取内容?

例如

     html 
     body#tplThread 
     div#mainContainer 
     div#main div#possd443.message 
     div.messageInner 
     div.messageContent 
     div.messageContentInner 
     h3#postTops443.messageTitle span

【问题讨论】:

  • CSS表示层叠样式 Sheet,所以不能get内容

标签: php css parsing path file-get-contents


【解决方案1】:

当然。

Domdocument 是要走的路:

function makeDom( $html_code ) {
$dom = new \DOMDocument();
$dom->LoadHTML($html_code, LIBXML_NONET);
$xpath = new \DOMXPath($this->dom);
return $xpath
}

比使用:

$xpath->query('//div[@id="desiredid"]/a[@href="desiredhref"]')

这里有一个很棒的 Xpath 语法教程: http://www.w3schools.com/xpath/xpath_syntax.asp

如果您要选择的项目有一个 id,您甚至不需要完整路径,使用 nodename 和 id 选择就足够了,因为 id 在整个页面中是唯一的。

【讨论】:

    猜你喜欢
    • 2018-11-29
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-21
    • 1970-01-01
    相关资源
    最近更新 更多