代码之家  ›  专栏  ›  技术社区  ›  user7747472

如何使用PHP-simple-HTML DOM解析器获取标记的属性

  •  2
  • user7747472  · 技术社区  · 7 年前

    PHP Simple HTML DOM parser 要废弃网站数据,但不幸的是无法提取我想要的数据。我也试过用谷歌搜索文档,但没能解决这个问题。我想要废弃的代码结构是这样的。

    <div id="section1">
       <h1>Some content</h1>
       <p>Some content</p>
       ............
        <<Not fixed number of element>>
       ............
       <script> <<Some script>></script>
       <video>
         <source src="www.exmple.com/34/exmple.mp4">
       </video>
    </div>
    

    我试过用JavaScript,我也可以这样做

    document.getElementById("section1").getElementsByTagName("source")[0].getAttribute("src");
    

    但是当我尝试使用PHP Dom解析器时,我没有得到任何数据。

    require ''.$_SERVER['DOCUMENT_ROOT'].'/../lib/simplehtmldom/simple_html_dom.php';
    
     $html_content = get($url); //This is cURL function to get website content.
     $obj_content = str_get_html($html_content);
     $linkURL = $obj_content->getElementById('section1')->find('source',0)->getAttribute('src');
    var_dump($linkURL); 
    

    这将导致空字符串。我也试着在这里和那里修改代码,但每次都没有一个是空白的。但是如果我 var dump $obj_content

    我试着从stackoverflow跟踪这些与我相似的帖子,但这些并没有帮助我。

    1. How do I get the HTML code of a web page in PHP?
    2. PHP Simple HTML DOM
    3. PHP Simple HTML DOM Parser Call to a member function children() on a non-object
    4. 还有他们的手册 http://simplehtmldom.sourceforge.net/manual.htm

    谢谢你

    1 回复  |  直到 7 年前
        1
  •  0
  •   user7747472    6 年前

    代码片段还不错。问题是我的目标URL在页面加载时不在那里。它是由 <script> 页面加载后的标记。

    推荐文章