我需要一个无头浏览器来抓取 CSS 属性吗
Do I need a headless browser to scrape for CSS attributes
我的目标是从网页中提取特定的 CSS 属性值。我已经使用 Guzzle 和 Symfony 的 css-selector 设置了一个 scraper。但是,据我所知,css-选择器的工作方式与 jQuery 不同,因为没有 .attr()
方法。
我认为我需要使用无头浏览器,mink,无头chrome,phantom.js,以便呈现页面,然后找到属性吗?
Mink 是一个不错的选择,因为它提供 api 以及允许与多个驱动程序交互的功能(goutte,gecko/firefox...)。
如果生成的css没有被javascript修改,mink+goutte可能是最好的选择,但是如果css被javascript以某种方式修改了mink+selenium 配置可能是最好的(或 mink+zombie)。请记住,第二种方法比 "goutte" 更难设置且更慢。
您访问 dom 的方式与 jQuery 不同,但选择器大致相同,事实上 mink 为您提供了 4 种类型的选择器。
您几乎可以用 "xpath" 选择器做任何事情。我还建议考虑 "css" + NodeElement 方法,因为它更简单并且在大多数情况下都有帮助。
这是一个基于 2 种方法的维基百科示例:
假设您前往 wikiperia.org 并希望保留英文条目 link:
$xPath = '//a[@id="js-link-box-en"]/@href';
$nodeElement = $this->getSession()->getPage()->find('xpath', $xPath);
$theHrefValue = $nodeElement->getText();
或者:
$nodeElement = $this->getSession()->getPage()->find('css', '#js-link-box-en')
$theHrefValue = $nodeElement->getAttribute('href');
希望对你做决定有所帮助:)
PhantomJS (http://phantomjs.org/) 是我用于单元测试的一个很好的工具。
Chrome 刚刚在 v59 中发布 运行 他们的浏览器处于无头模式的能力。但是它确实适用于 windows。
Headless Chrome is shipping in Chrome 59. It's a way to run the Chrome
browser in a headless environment. Essentially, running Chrome without
chrome! It brings all modern web platform features provided by
Chromium and the Blink rendering engine to the command line.
Why is that useful?
A headless browser is a great tool for automated testing and server
environments where you don't need a visible UI shell. For example, you
may want to run some tests against a real web page, create a PDF of
it, or just inspect how the browser renders an URL.
Caution: Headless mode is available on Mac and Linux in Chrome 59.
Windows support is coming in Chrome 60. To check what version of
Chrome you have, open chrome://version.
您可以在此处找到更多信息:https://developers.google.com/web/updates/2017/04/headless-chrome
我的目标是从网页中提取特定的 CSS 属性值。我已经使用 Guzzle 和 Symfony 的 css-selector 设置了一个 scraper。但是,据我所知,css-选择器的工作方式与 jQuery 不同,因为没有 .attr()
方法。
我认为我需要使用无头浏览器,mink,无头chrome,phantom.js,以便呈现页面,然后找到属性吗?
Mink 是一个不错的选择,因为它提供 api 以及允许与多个驱动程序交互的功能(goutte,gecko/firefox...)。
如果生成的css没有被javascript修改,mink+goutte可能是最好的选择,但是如果css被javascript以某种方式修改了mink+selenium 配置可能是最好的(或 mink+zombie)。请记住,第二种方法比 "goutte" 更难设置且更慢。
您访问 dom 的方式与 jQuery 不同,但选择器大致相同,事实上 mink 为您提供了 4 种类型的选择器。
您几乎可以用 "xpath" 选择器做任何事情。我还建议考虑 "css" + NodeElement 方法,因为它更简单并且在大多数情况下都有帮助。
这是一个基于 2 种方法的维基百科示例:
假设您前往 wikiperia.org 并希望保留英文条目 link:
$xPath = '//a[@id="js-link-box-en"]/@href';
$nodeElement = $this->getSession()->getPage()->find('xpath', $xPath);
$theHrefValue = $nodeElement->getText();
或者:
$nodeElement = $this->getSession()->getPage()->find('css', '#js-link-box-en')
$theHrefValue = $nodeElement->getAttribute('href');
希望对你做决定有所帮助:)
PhantomJS (http://phantomjs.org/) 是我用于单元测试的一个很好的工具。
Chrome 刚刚在 v59 中发布 运行 他们的浏览器处于无头模式的能力。但是它确实适用于 windows。
Headless Chrome is shipping in Chrome 59. It's a way to run the Chrome browser in a headless environment. Essentially, running Chrome without chrome! It brings all modern web platform features provided by Chromium and the Blink rendering engine to the command line.
Why is that useful?
A headless browser is a great tool for automated testing and server environments where you don't need a visible UI shell. For example, you may want to run some tests against a real web page, create a PDF of it, or just inspect how the browser renders an URL.
Caution: Headless mode is available on Mac and Linux in Chrome 59. Windows support is coming in Chrome 60. To check what version of Chrome you have, open chrome://version.
您可以在此处找到更多信息:https://developers.google.com/web/updates/2017/04/headless-chrome