我们可以在DomDocument中使用Array Element并嵌套xpath吗?

发布于 2025-01-02 21:21:43 字数 1169 浏览 0 评论 0原文

我使用以下代码

    $page_data=array();//$title_links is array having urls
     $nodearr=array();
    foreach ($title_links as $b_url)
    {
        $page_data[]= mycurl($b_url);//my curl function, it is okay

        $dom2 = new DOMDocument();//creating new domdocument object inside a domdocument object
        @$dom2->loadHTML($page_data[]); 
        $xpath_cat = new DOMXPath($dom2);//same as domdocument , nesting xpath
        $nodelist = $xpath_cat->query('//span[@class="zg_hrsr_ladder"]', $dom2);
//echo nodeslist;

foreach ($nodelist as $node) {
   $nodearr[] = $node->textContent;
 }


    }

,所以场景是,

  • 我将一个“domdocument”嵌套在另一个“DomDocument”中
  • 使用“foreach 循环”,我在 DomDocument 中插入数组元素
  • aslo,因为我将 domdocument 嵌套在 domdocument 中,所以我是 声明新的 xpath 查询

  • 问题是...

  • 这样做可以吗?这是可接受的编程技术吗?
  • 优点和缺点?
  • 有什么替代方案吗?

    <块引用>

    澄清:

  • $title_links是使用curl获取的,它是url 使用 domdocument 和 xpath 查询从网页中提取。
  • 所以 $title_links 位于 xpath 和 domdocument 的第一层,$nodelist 位于 od xpath 和 dom-document 嵌套的第二层

  • I am using the following code

        $page_data=array();//$title_links is array having urls
         $nodearr=array();
        foreach ($title_links as $b_url)
        {
            $page_data[]= mycurl($b_url);//my curl function, it is okay
    
            $dom2 = new DOMDocument();//creating new domdocument object inside a domdocument object
            @$dom2->loadHTML($page_data[]); 
            $xpath_cat = new DOMXPath($dom2);//same as domdocument , nesting xpath
            $nodelist = $xpath_cat->query('//span[@class="zg_hrsr_ladder"]', $dom2);
    //echo nodeslist;
    
    foreach ($nodelist as $node) {
       $nodearr[] = $node->textContent;
     }
    
    
        }
    

    so the scenerio is ,

  • I am nesting a 'domdocument' inside an other 'DomDocument'
  • using 'foreach loop', i am inserting Array Element in DomDocument

  • aslo, as i am nestind domdocument inside domdocument , so i am
    declaring new xpath query

  • The Question is ...

  • Doing so is okay? Is it a acceptable Programming technique?
  • Pros and Cons?
  • Any ALternative to it?

    Clarification:

  • $title_links is obtained using curl, it is urls
    extracted from web page using domdocument and xpath query.

  • so
    $title_links is at first level of xpath and domdocument, and $nodelist
    is at the 2nd level of nesting od xpath and dom-document

  • 如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(1

    江南月 2025-01-09 21:21:43

    失败的原因是: @$dom2->loadHTML($page_data[]);

    [] 只能用于赋值,不能用于读取。您实际上需要有一个可以参考的密钥。我可能会这样做:

    $page_data=array();//$title_links is array having urls
    foreach ($title_links as $key => $b_url)
    {
        $page_data[$key]= mycurl($b_url);//my curl function, it is okay
    
        $dom2 = new DOMDocument();
        $dom2->loadHTML($page_data[$key]); // error suppression is evil!
        $xpath_cat = new DOMXPath($dom2);
    
    }
    

    我仍然不清楚这里到底是如何发生嵌套的;你没有嵌套任何东西,你只是迭代一个数组。

    The reason it's failing is this: @$dom2->loadHTML($page_data[]);

    [] can only be used for assignment, not reading. You need to actually have a key you can reference. I would probably do it like this:

    $page_data=array();//$title_links is array having urls
    foreach ($title_links as $key => $b_url)
    {
        $page_data[$key]= mycurl($b_url);//my curl function, it is okay
    
        $dom2 = new DOMDocument();
        $dom2->loadHTML($page_data[$key]); // error suppression is evil!
        $xpath_cat = new DOMXPath($dom2);
    
    }
    

    What's still unclear to me is how exactly there is any nesting going on here; you're not nesting anything, you are just iterating over an array.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文