首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >nokogiri:如何在给定的xpath元素周围包装html标签?

nokogiri:如何在给定的xpath元素周围包装html标签?
EN

Stack Overflow用户
提问于 2009-10-18 05:20:09
回答 1查看 1.7K关注 0票数 3

我有一个xpath来抓取没有被任何html标签包围的每个文本节点。相反,它们是通过<br>分开的。我想用<span>标签把这些包起来。

代码语言:javascript
复制
Nokogiri::HTML(open("http://vancouver.en.craigslist.ca/van/swp/1426164969.html"))
.xpath("//br/following-sibling::text()|//br/preceding-sibling::text()").to_a

将返回这些文本节点。

完成以下修订后的代码:

代码语言:javascript
复制
doc = Nokogiri::HTML(open("http://vancouver.en.craigslist.ca/van/swp/1426164969.html"))
.xpath("//br/following-sibling::text()|//br/preceding-sibling::text()").wrap("<span></span>")
puts doc

我希望看到一个完整的html源代码,其中包含用<span>标记包装的文本,但我得到了以下内容:

代码语言:javascript
复制
Date: 2009-10-17,  4:36PM PDT
Reply to:
This is a spectacular open plan 1000 sq. ft. loft is in a former Canada Post building. Upon entering the loft from the hallway you are amazed at where you have arrived.... a stunning, bright and fully renovated apartment that retains its industrial feel. The restoration of the interior was planned and designed by a famous Vancouver architect.
The loft is above a police station, so youÂre guaranteed peace and quite at any time of the day or night.
The neighborhood is safe and lively with plenty of restaurants and shopping. ThereÂs a starbucks across the street and plenty of other coffee shops in the area.  Antique alley with its hidden treasures is one block away, as well as the beautiful mile long boardwalk. Skytrain station is one minute away (literally couple of buildings away). 15 minutes to Commercial drive, 20 minutes to downtown Vancouver and Olympic venues.
Apartment Features:
-       Fully furnished
-       14 ft ceilings
-       Hardwood floors
-       Gas fireplace
-       Elevator
-       Large rooftop balcony
-       Full Kitchen: Fully equipped with crystal, china and utensils
-       Dishwasher
-       Appliances including high-end juice maker, blender, etc.
-       WiFi (Wireless Internet)
-       Bathtub
-       Linens &amp; towels provided
-       Hair dryer
-       LCD Flat-screen TV with DVD player
-       Extensive DVD library
-       Music Library: Ipod connection
-       Wii console with Guitar Hero, games
-       Book and magazine library
-       Non-smoking
We are looking to exchange for a place somewhere warm (California, Hawaii, Mexico, South America, Central America) or a place in Europe (UK, Italy, France).
Email for other dates and pictures of the loft.
EN

回答 1

Stack Overflow用户

发布于 2009-10-25 17:00:46

您的文档变量没有赋值给整个文档-您应该使用

代码语言:javascript
复制
doc = Nokogiri::HTML(open("http://vancouver.en.craigslist.ca/van/swp/1426164969.html"))
doc.xpath("//br/following-sibling::text()|//br/preceding-sibling::text()").wrap("<span></span>")
puts doc

不幸的是,它没有解决这个问题,因为nokogiri将所有brs放在第一位,而不是所有跨度,文本如下:

代码语言:javascript
复制
<br><br><br><br><span>
text</span><span>
text</span><span>
text</span><span>
text</span>

但是你可以这样做

代码语言:javascript
复制
doc = Nokogiri::HTML(open("http://vancouver.en.craigslist.ca/van/swp/1426164969.html"))
doc.search("//br/following-sibling::text()|//br/preceding-sibling::text()").each do |node|
  node.replace(Nokogiri.make("<span>#{node.to_html}</span>"))
end
puts doc
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1584080

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档