从 ruby​​ 中的 HTML 元素中提取背景图像 [英] Extract background-image from an HTML element in ruby

查看:46
本文介绍了从 ruby​​ 中的 HTML 元素中提取背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Nokogiri 从 div 中提取 background-url,但无法解析它的 background-url.

I am trying to extract background-url from a div using Nokogiri but am not able parse background-url of it.

在 StackOverflow 上搜索时,我找到了这个链接
解析:我可以接吗Nokogiri 中嵌入的 CSS 背景的 URL?
但那里给出的解决方案不起作用.

While Searching on StackOverflow I found this link
Parsing: Can I pick up the URL of embedded CSS Background in Nokogiri?
but the solution given there doesn't work.

推荐答案

Nokogiri 不是网络浏览器.它位于 libxml2 之上,提供快速且出色的 XML 和 HTML 解析,以及从中操作和提取数据.

Nokogiri is not a web browser. It stands on top of libxml2 to provide fast and excellent parsing of XML and HTML, and manipulation and extraction of data from this.

它只处理网页中的 HTML.它不运行任何 JavaScript.它不会将 CSS 应用于 DOM.没有办法使用 Nokogiri 来查找应用于元素的 CSS 样式,除非它直接位于该元素的 style="..." 属性上.(即使这样,您也需要使用其他东西,例如正则表达式,来解析其中的 CSS.)

It only deals with the HTML in a web page. It does not run any JavaScript. It does not apply CSS to the DOM. There is no way to use Nokogiri to find a CSS style applied to an element unless it is directly on the style="..." attribute on that element. (And even then you would need to use something else, like regex, to parse the CSS therein.)

你会想要使用其他东西,比如由 Ruby 控制的无头浏览器,例如Watir 或 Selenium,如果您想处理网页并像浏览器一样对待它.

You will want to use something else, like a headless browser controlled by Ruby, e.g. Watir or Selenium, if you want to process a web page and treat it like a web browser does.

这篇关于从 ruby​​ 中的 HTML 元素中提取背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆