gpt4 book ai didi

php - 我如何找到这个 div ? (PHP 简单 HTML DOM 解析器)

转载 作者:太空狗 更新时间:2023-10-29 15:07:11 25 4
gpt4 key购买 nike

这是我的代码:

<?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;
?>

但问题是,如果我加载该页面,它只会显示以下内容:

Notice: Trying to get property of non-object in C:\xampp\htdocs...\simpletest2.php on line 4

那么我怎样才能得到我正在搜索的 div 以及我做错了什么?

编辑:

Solution:

<?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;
?>

Or:

<?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;
?>

最佳答案

我在搜索类(class)的地方更改了您的代码:

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

我查找了类为 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]

关于php - 我如何找到这个 div ? (PHP 简单 HTML DOM 解析器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47814264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com