使用 Nokogiri 查找 id 包括 [] 的标签 [英] Find tag with id including [] with Nokogiri

查看:37
本文介绍了使用 Nokogiri 查找 id 包括 [] 的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 html 元素,例如:

我想通过 id 搜索这个元素,但似乎 nokogiri 被 [] 弄糊涂了.我正在尝试:

doc.css("#spam[#{eggs.id}]")

但无济于事.

解决方案

Chris,试试这个,让我知道它是否有效:

doc = Nokogiri::HTML(page)el = doc.xpath("//div[@id='spam[500]']").first

问题是您无法通过 CSS 访问它(即使在浏览器中).尝试为spam[500]"设置一些 CSS 属性,但它们不会被应用.但是,您可以通过 xpath 访问,如上所示.

I have an html element like:

<div id="spam[500]">

I want to search for this element by id, but it seems that nokogiri is getting confused by the []. I'm trying:

doc.css("#spam[#{eggs.id}]")

but to no avail.

解决方案

Chris, try this and let me know if it works:

doc = Nokogiri::HTML(page)
el = doc.xpath("//div[@id='spam[500]']").first

The problem is that you can't access it via CSS (even in the browser). Try setting some CSS attributes for "spam[500]" and they won't be applied. You can access via xpath however, as shown above.

这篇关于使用 Nokogiri 查找 id 包括 [] 的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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