php - 在简单的 html dom 中按类和 id 选择在谷歌搜索中不起作用
问题描述
我尝试按类查找 div 的以下代码不适用于谷歌搜索结果,我也尝试过id。
include('simple_html_dom.php');
$dom = file_get_html("https://www.google.com/search?q=best+mug");
$all_divs = $dom->find("div[class='g']");
foreach ($all_divs as $div) {
echo $div->plaintext;
}
解决方案
我认为最好使用 XPath 来做到这一点,下面是使用 XPath 的代码示例:
$dom = file_get_contents("https://www.google.com/search?q=best+mug");
@$doc = new DOMDocument();
@$doc->loadHTML($dom);
$xpath = new DomXPath($doc);
$all_divs = $xpath->query("//div[@class='g']");
foreach ($all_divs as $div) {
echo $div->plaintext;
}
试试看,让我知道它是否有效。
推荐阅读
- php - OPCache 处理多个 apache
- sql-server - 在 SSRS 矩阵报告中添加 2 个计算字段时出现问题
- configuration - 在 ack 中,我可以定义一个受扩展名和第一行限制的类型(两个过滤器类型)吗?
- java - 使用 postgresql NOT IN 语句
- python - 有效地折叠 Parquet 中的行组
- python - 添加相似数组的值
- javascript - 如何为 Javascript 视频播放触发器添加延迟
- php - 无法在 Heroku 上移动上传的文件
- shell - 使文件和目录恢复生机
- java - 如何解决此代码中使用泛型的“方法不适用于参数”问题