亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 simplehtmldom 從此頁面提取數據

如何使用 simplehtmldom 從此頁面提取數據

PHP
德瑪西亞99 2023-09-08 16:42:04
我正在嘗試使用 simplehtmldom 從https://benthamopen.com/browse-by-title/B/1/中提取信息。具體來說,我想訪問頁面的以下部分:<div style="padding:10px;"><strong>ISSN: </strong>1874-1207<br><div class="sharethis-inline-share-buttons" style="padding-top:10px;" data-url="https://benthamopen.com/TOBEJ/home/" data-title="The Open Biomedical Engineering Journal"></div></div>我有這個代碼:$html = file_get_html('https://benthamopen.com/browse-by-title/B/1/');foreach($html->find('div[style=padding:10px;]') as $ele) {    echo("<pre>".print_r($ele,true)."</pre>");}...返回(我只顯示頁面中的一項)simplehtmldom\HtmlNode Object(    [nodetype] => HDOM_TYPE_ELEMENT (1)    [tag] => div    [attributes] => Array        (            [style] => padding:10px;        )    [nodes] => Array        (            [0] => simplehtmldom\HtmlNode Object                (                    [nodetype] => HDOM_TYPE_ELEMENT (1)                    [tag] => strong                    [attributes] => none                    [nodes] => none                )            [1] => simplehtmldom\HtmlNode Object                (                    [nodetype] => HDOM_TYPE_TEXT (3)                    [tag] => text                    [attributes] => none                    [nodes] => none                )            [2] => simplehtmldom\HtmlNode Object                (                    [nodetype] => HDOM_TYPE_ELEMENT (1)                    [tag] => br                    [attributes] => none                    [nodes] => none                )我不確定如何從這里繼續。我想提?。篒SSN 文本(在 echo 語句中沒有顯示 - 不確定為什么)[上例中的 1874-1207]。它是 [nodes] 的元素零'data-url' [https://benthamopen.com/TOBEJ/home/,在上面的示例中]“數據標題”[開放生物醫學工程雜志,在上面的例子中]也許我對PHP對象和數組的理解還不夠好,我不知道為什么echo語句中沒有顯示ISSN。我嘗試了各種(很多)方法,但只是努力從元素中提取數據。
查看完整描述

1 回答

?
森林海

TA貢獻2011條經驗 獲得超2個贊

我對 simplehtmldom 不熟悉,除了知道避免它之外。因此,我將提出一個使用 PHP 內置 DOM 類的解決方案:


<?php

libxml_use_internal_errors(true);

// get the HTML

$html = file_get_contents("https://benthamopen.com/browse-by-title/B/1/");


// create a DOM object and load it up

$dom = new DomDocument();

$dom->loadHtml($html);


// create an XPath object and query it

$xpath = new DomXPath($dom);

$elements = $xpath->query("//div[@style='padding:10px;']");


// loop through the matches

foreach ($elements as $el) {

    // skip elements without ISSN

    $text = trim($el->textContent);

    if (strpos($text, "ISSN") !== 0) {

        continue;

    }

    // get the first div inside this thing

    $div = $el->getElementsByTagName("div")[0];

    // dump it out

    printf("%s %s %s<br/>\n", str_replace("ISSN: ", "", $text), $div->getAttribute("data-title"), $div->getAttribute("data-url"));

}

XPath 的內容可能有點讓人不知所措,但對于像這樣的簡單搜索,它與 CSS 選擇器沒有太大區別。希望評論能解釋一切,如果沒有,請告訴我!


輸出:


1874-1207 The Open Biomedical Engineering Journal https://benthamopen.com/TOBEJ/home/<br/>

1874-1967 The Open Biology Journal https://benthamopen.com/TOBIOJ/home/<br/>

1874-091X The Open Biochemistry Journal https://benthamopen.com/TOBIOCJ/home/<br/>

1875-0362 The Open Bioinformatics Journal https://benthamopen.com/TOBIOIJ/home/<br/>

1875-3183 The Open Biomarkers Journal https://benthamopen.com/TOBIOMJ/home/<br/>

2665-9956 The Open Biomaterials Science Journal https://benthamopen.com/TOBMSJ/home/<br/>

1874-0707 The Open Biotechnology Journal https://benthamopen.com/TOBIOTJ/home/<br/>


查看完整回答
反對 回復 2023-09-08
  • 1 回答
  • 0 關注
  • 111 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號