javascript - 使用 PHP Simple HTML DOM Parser 抓取脚本生成的文本

标签 javascript php html web-scraping

我正在尝试获取以下代码中显示的文本“Huggies Pure Baby Wipes 4 x 64 per pack”。

<div class="offerList-item-description-title">
    <div id="result-title-5" class="offerList-item-description-title">
        <script type="text/javascript">
            document.write(getContents('wF8UD9Jj8:6D !FC6 q23J (:A6D c I ec A6C A24\<'));
        </script>Hug­gies Pure Baby Wipes 4 x 64 per pack
    </div>
</div>

我尝试过使用以下代码:

foreach($element -> find('.offerList-item-description-title') as $title)
{
    foreach($element -> find('text') as $text){
        echo $text;
    }
}

但只是返回一个空字符串,有什么建议吗?

谢谢。

最佳答案

如果您知道您的抓取工具返回的 HTML 不包含 Javascript 呈现的代码,就像您的情况一样,文本是由 javascript 生成的,这就是您收到空响应的原因。你需要的是像 PhantomJS 这样的 headless 浏览器,你可以使用 PhantomJS 的 PHP 包装器 http://jonnnnyw.github.io/php-phantomjs/ .

这将解决您的问题。它具有以下特点:

  • 通过 PhantomJS headless 浏览器加载网页
  • 查看详细的响应数据,包括页面内容、标题、状态 代码等
  • 处理重定向
  • 查看 JavaScript 控制台错误

希望这有帮助。

关于javascript - 使用 PHP Simple HTML DOM Parser 抓取脚本生成的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49277502/

相关文章:

javascript - jquery.mockjax 与需要 shim 的 Webpack 一起使用

javascript - bootstrap 通过js获取输入复选框的值

php - 使用 htaccess 的 Zend 框架站点的 SEO 友好 url

php - 使用 php 解析 mysql 转储文件中的值?

javascript - 认为我的 ajax 有问题吗?

javascript - 如何防止 html 元素转义父元素? (安全)

javascript - 通过表单发送多个<option>

javascript - 无法让导航栏停止显示水平滚动条

c# - Windows 8 C# - 以字符串形式检索网页源

javascript - 在 Windows 8 中更改视频标签的来源