【发布时间】:2015-04-20 01:05:12
【问题描述】:
鉴于 HTML 页面的以下部分,我希望能够将“我们”和“约翰”分开对待。
<div id="ad-description" class="c-clear c-word-wrap">
Room for rent in Sydney.
<br/><br/>For more information please contact us<br/>John :- 0491 570 156<br/>Jane :- (02) 5550 1234</div>
<!-- google_ad_section_end(name=description) -->
</div>
当使用 Nokogiri 访问广告描述节点,然后在该节点上调用 content 时,我得到 usJohn 作为结果字符串的一部分:
document = Nokogiri::HTML(text)
ad_description_xpath = './/div[contains(@id, "ad-description")]'
ad_description_nodes = document.xpath(ad_description_xpath)
ad_description_node = ad_description_nodes.first
ad_description_node.content # "...please contact usJohn :- ..."
如何让 Nokogiri 返回一个在“us”和“John”之间带有某种空格的字符串,或者在单独的字符串中使用“us”和“John”?
理想情况下,所采用的方法将能够处理节点内的任何标签,而我编写的代码不必提及特定标签。
【问题讨论】:
标签: ruby web-scraping nokogiri