第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何使用 simplehtmldom 從此頁面提取數(shù)據(jù)

如何使用 simplehtmldom 從此頁面提取數(shù)據(jù)

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>我有這個(gè)代碼:$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>");}...返回(我只顯示頁面中的一項(xiàng))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                )我不確定如何從這里繼續(xù)。我想提取:ISSN 文本(在 echo 語句中沒有顯示 - 不確定為什么)[上例中的 1874-1207]。它是 [nodes] 的元素零'data-url' [https://benthamopen.com/TOBEJ/home/,在上面的示例中]“數(shù)據(jù)標(biāo)題”[開放生物醫(yī)學(xué)工程雜志,在上面的例子中]也許我對(duì)PHP對(duì)象和數(shù)組的理解還不夠好,我不知道為什么echo語句中沒有顯示ISSN。我嘗試了各種(很多)方法,但只是努力從元素中提取數(shù)據(jù)。
查看完整描述

1 回答

?
森林海

TA貢獻(xiàn)2011條經(jīng)驗(yàn) 獲得超2個(gè)贊

我對(duì) simplehtmldom 不熟悉,除了知道避免它之外。因此,我將提出一個(gè)使用 PHP 內(nèi)置 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 的內(nèi)容可能有點(diǎn)讓人不知所措,但對(duì)于像這樣的簡(jiǎn)單搜索,它與 CSS 選擇器沒有太大區(qū)別。希望評(píng)論能解釋一切,如果沒有,請(qǐng)告訴我!


輸出:


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/>


查看完整回答
反對(duì) 回復(fù) 2023-09-08
  • 1 回答
  • 0 關(guān)注
  • 109 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)