问题描述
这是我的代码:
<?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搜索页面的所有结果。
It outputs all result of the Google Search Page under the Search "BA236".
问题是我不需要所有这些信息,我需要的信息是在没有id或类或其他任何内容的div中。
The problem is I dont need all of them and the Information I need is inside a div that has no id or class or anything else.
我需要的div在第一个
The div I need is inside the first
<div class="g">
在页面上,所以也许我应该试试这样的内容:
on the Page, so maybe I should try something like this:
<?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;
?>
但问题是,如果我加载页面, p>
But the Problem of that is, if I load the page it shows me nothing except this:
那么我怎样才能得到div我正在寻找和我做错了什么?
So how can i get the div i´m searching for and what am I doing wrong ?
编辑:
$ b
<?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;
?>
推荐答案
我对您的代码进行了更改对于这个类:
I made a change to your code where I am searching for the 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;
?>
结果:
result:
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'的计数
I looked for the div elements with class g then I printed the count of the first element '0'
$e = $html-> find ("div [class = g]");
echo $e [0]->innertext;
您的代码:
Your code:
<?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') code>
但('div [class = g]')[0]
这篇关于我如何找到这个div? (PHP简单的HTML DOM解析器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!