这是我的代码:
<?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;
?>
Run Code Online (Sandbox Code Playgroud)
它会在搜索"BA236"下输出Google搜索页面的所有结果.
问题是我不需要所有这些,我需要的信息是在没有id或类或其他任何内容的div中.
我需要的div是第一个
<div class="g">
Run Code Online (Sandbox Code Playgroud)
在页面上,所以也许我应该尝试这样的事情:
<?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;
?>
Run Code Online (Sandbox Code Playgroud)
但问题是,如果我加载页面,它除了显示我之外什么都没有:
注意:尝试在第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;
?>
Run Code Online (Sandbox Code Playgroud)
要么:
<?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;
?>
Run Code Online (Sandbox Code Playgroud)
我对您正在搜索课程的代码进行了更改:
\n\n<?php\n include(\'simple_html_dom.php\');\n $html = file_get_html(\'http://www.google.com/search?q=BA236\',false);\n $e = $html->find("div[class=g]");\necho $e[0]->innertext;\n?>\nRun Code Online (Sandbox Code Playgroud)\n\n结果:
\n\nBritish Airways Flight 236\n\nScheduled departs in 13 hours 13 mins\n\nDeparture DME 5:40 AM \xe2\x80\x94\n\nMoscow Dec 15 \n\nArrival LHR 6:55 AM Terminal 5\n\nLondon Dec 15 \n\nScheduled departs in 1 day 13 hours\n\nDeparture DME 5:40 AM \xe2\x80\x94\n\nMoscow Dec 16 \n\nArrival LHR 6:55 AM Terminal 5\n\nLondon Dec 16 \nRun Code Online (Sandbox Code Playgroud)\n\n我查找了 g 类的 div 元素,然后打印了第一个元素 \'0\' 的计数
\n\n$e = $html-> find ("div [class = g]");\necho $e [0]->innertext;\nRun Code Online (Sandbox Code Playgroud)\n\n你的代码:
\n\n<?php\n include(\'simple_html_dom.php\');\n $html = file_get_html(\'http://www.google.com/search?q=BA236\',false);\n $title=$html->find(\'div[class=g]\')[0]->innertext;\n echo $title;\n?>\nRun Code Online (Sandbox Code Playgroud)\n\n不是(\'div[class=g], 0\')
但(\'div[class=g]\')[0]
| 归档时间: |
|
| 查看次数: |
794 次 |
| 最近记录: |