【问题标题】:php simple html dom get a href id inside tdphp 简单的 html dom 在 td 中获取一个 href id
【发布时间】:2018-02-21 20:38:05
【问题描述】:

如何使用 php simple html dom 在 href 中获取“name”或“id”属性。我还需要“h4”标签内的“标题文本”。 请你帮助我好吗? 这是html:

<td>
<a href="../Vehicle?itemID=22995061&RowNumber=9&loadRecent=True" name="22995061" id="22995061">
<h4>title text</h4>
</a>
<p>
Stock#:
<text>example</text>
</p>
<p>BLA BLA</p>
<p> fffff  </p>
</td>

我尝试了类似的方法,但它返回空白。

IDs = array();  
    $url = "http://someurl";
    $html = file_get_html(url);
foreach($html->find('h4') as $e)
 {

     echo $e->innertext."<br>";
     $dataID = $e->innertext; 
     $IDs[] = $dataID; 

 }

【问题讨论】:

    标签: php html dom find href


    【解决方案1】:

    首先,改变,

    IDs = array();  
    

    到,

    $IDs = array();  
    

    那么,为什么不使用DOMDocument 类而不是正则表达式。只需加载您的 DOM,然后使用getElementsByTagName 来获取您的标签。通过这种方式,您可以排除任何其他您不想要的标签,而只获取您想要的标签。

    示例

    <?php
    $xml = <<< XML
    <?xml version="1.0" encoding="utf-8"?>
    <books>
     <book>Patterns of Enterprise Application Architecture</book>
     <book>Design Patterns: Elements of Reusable Software Design</book>
     <book>Clean Code</book>
    </books>
    XML;
    
    $dom = new DOMDocument;
    $dom->loadXML($xml);
    $books = $dom->getElementsByTagName('book');
    foreach ($books as $book) {
        echo $book->nodeValue, PHP_EOL;
    }
    ?>
    

    阅读材料

    DOMDocument

    【讨论】:

    • 嗨,tnx 的回复,但我使用 php simple html dom 来获取数据,因为我在代理后面。当我在代理服务器后面时,我不知道如何使用 php DOM 来获取 url。
    猜你喜欢
    • 1970-01-01
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    相关资源
    最近更新 更多