【问题标题】:using simple_html_dom with ajax [duplicate]将 simple_html_dom 与 ajax 一起使用 [重复]
【发布时间】:2015-04-11 21:40:52
【问题描述】:

我正在使用“simple_html_dom”库对页面进行解析,但是无法成功解析其内容是通过 ajax 获得的 html。有没有办法解决这个问题?

PHP 代码:

<?php
require_once '../library/Simple_HTML_DOM/simple_html_dom.php';

// Create DOM from URL or file
$html = file_get_html('http://www.playnow3dgames.com/genre.php?id=sports');

// Find all images 
foreach($html->find('img') as $element){
echo $element->src . '<br>';
}
?>

仅打印边缘和顶部的图像(html 原生)并且不解析中心图像(使用 ajax)。

【问题讨论】:

  • 您的意思是您没有获得仅通过 JavaScript 添加到页面的内容?当然不是,因为file_get_contents 不会“执行” JavaScript。你需要一些模拟浏览器的东西(研究关键字:headless browser
  • 谢谢,期待 phamtomjs,希望对我有用

标签: php html ajax


【解决方案1】:

试试这个

<?php
require_once '../library/Simple_HTML_DOM/simple_html_dom.php';

// Create DOM from URL or file
$html = file_get_html('http://www.playnow3dgames.com/listing.php?genre=sports&order=date');

// Find all images 
foreach($html->find('img') as $element){
    echo $element->src . '<br>';
}
?>

=== 更新 ====

实际上,这是一个 iframe,它不是 ajax。 http://www.playnow3dgames.com/genre.php?id=sports 的中心是框架:http://www.playnow3dgames.com/listing.php?genre=sports&order=date

你可以看到 url 的结构:

http://www.playnow3dgames.com/listing.php?genre=sports&order=date

在这里:genre=sports

这是真实的网址:http://www.playnow3dgames.com/genre.php?id=sports

您将看到id=sportsgenre=sports 匹配

要获取每一页,您只需更改genre=genre_name。例如:

http://www.playnow3dgames.com/genre.php?id=strategy

主框架将是:

www.playnow3dgames.com/listing.php?genre=strategy&order=date

如果您想获取第 1,2,3... 页,您需要添加page=page_number。例如:获取第 2 页的

http://www.playnow3dgames.com/genre.php?id=strategy

网址将是:

http://www.playnow3dgames.com/listing.php?genre=strategy&page=2&order=date

【讨论】:

  • 感谢您的回答,@MrJerry,第一个这个listing.php 在哪里?第二个作品,但适用于每个使用 ajax 的网站?
  • 查看我的更新以获取每一页
猜你喜欢
  • 2011-09-03
  • 2015-11-24
  • 2014-06-03
  • 2011-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-05
相关资源
最近更新 更多