繁体   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