簡體   English   中英

我怎么找到這個div? (PHP Simple HTML DOM Parser)

[英]How do I find this div ? (PHP Simple HTML DOM Parser)

這是我的代碼:

<?php
    include('simple_html_dom.php');
    $html = file_get_html('http://www.google.com/search?q=BA236',false);
    $title=$html->find('div#ires', 0)->innertext;
    echo $title;
?>

它會在搜索“BA236”下輸出Google搜索頁面的所有結果。

問題是我不需要所有這些,我需要的信息是在沒有id或類或其他任何內容的div中。

我需要的div是第一個

<div class="g">

在頁面上,所以也許我應該嘗試這樣的事情:

<?php
    include('simple_html_dom.php');
    $html = file_get_html('http://www.google.com/search?q=BA236',false);
    $title=$html->find('div[class=g], 0')->innertext;
    echo $title;
?>

但問題是,如果我加載頁面,它除了顯示我之外什么都沒有:

注意:嘗試在第4行的C:\\ xampp \\ htdocs ... \\ simpletest2.php中獲取非對象的屬性

那我怎么能得到我正在尋找的div以及我做錯了什么?

編輯:

解:

<?php
    include('simple_html_dom.php');
    $html = file_get_html('http://www.google.com/search?q=BA236',false);
    $e = $html->find("div[class=g]");
    echo $e[0]->innertext;
?>

要么:

<?php
    include('simple_html_dom.php');
    $html = file_get_html('http://www.google.com/search?q=BA236',false);
    $title=$html->find('div[class=g]')[0]->innertext;
    echo $title;
?>

我在代碼中對我的代碼進行了更改:

<?php
    include('simple_html_dom.php');
    $html = file_get_html('http://www.google.com/search?q=BA236',false);
    $e = $html->find("div[class=g]");
echo $e[0]->innertext;
?>

結果:

British Airways Flight 236

Scheduled   departs in 13 hours 13 mins

Departure   DME 5:40 AM     —

Moscow  Dec 15      

Arrival LHR 6:55 AM     Terminal 5

London  Dec 15      

Scheduled   departs in 1 day 13 hours

Departure   DME 5:40 AM     —

Moscow  Dec 16      

Arrival LHR 6:55 AM     Terminal 5

London  Dec 16      

我用class g查找了div元素然后我打印了第一個元素'0'的計數

$e = $html-> find ("div [class = g]");
echo $e [0]->innertext;

你的代碼:

<?php
    include('simple_html_dom.php');
    $html = file_get_html('http://www.google.com/search?q=BA236',false);
    $title=$html->find('div[class=g]')[0]->innertext;
    echo $title;
?>

('div[class=g], 0')

但是('div[class=g]')[0]

這里不需要simple_html_dom,使用內置DOMDocument和DOMXPath很容易。

<?php
$html = file_get_contents('http://www.google.com/search?q=BA236');
echo (new DOMXPath ( (@DOMDocument::loadHTML ( $html )) ))->query ( '//div[@class="g"]' )->item ( 0 )->textContent;

在我看來,DOMDocument + DOMXPath使simple_html_dom.php變得毫無意義。 前者2可以做幾乎所有simple_html_dom都可以做的事情,並且是內置的本機php函數,只要PHP本身被維護就可以維護,而后者是第三方項目,看起來幾乎死了它(最后一次提交是在2014年,2014年全部只提交了1次,2013年全部提交了2次)

我怎么找到最后一個<div class>在帶有 PHP 簡單 HTML DOM 解析器的 HTML 文件中?</div><div id="text_translate"><p> 根據<a href="http://simplehtmldom.sourceforge.net/" rel="nofollow noreferrer">SIMPLE HTML DOM PARSER</a>的文檔(在“How to modify HTML Elements”選項卡下),這段代碼找到了<div class="hello">的第一個實例:</p><pre> $html = str_get_html('<div class="hello">Hello</div><div class="world">World</div>'); $html->find('div[class=hello]', 0)->innertext = 'foo'; echo $html; // Output: <div class="hello">foo</div><div class="world">World</div></pre><p> 如果我想在<div class="hello">的<em>最后一個</em>實例中插入 'foo' 怎么辦,假設 HTML 代碼有很多<div class="hello">實例。</p><p> 什么應該取代0 ?</p></div>

[英]How do I find the last <div class> in an HTML File with PHP Simple HTML DOM Parser?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM