如何在Ruby on Rails中使用Nokogiri从HTML中查找代表性图像

Eri*_*ric 1 html xslt xpath ruby-on-rails nokogiri

我需要放弃使用xsltproc命令行工具在Heroku上进行部署,因为他们并不真正支持它.Nokogiri宝石看起来应该适用于我需要的一切,尽管我无法从HTML中找到代表性的图像.

我的意思是代表性的图像是,/ html/body下的第一个图像在链接中有"://"并且没有"广告".或"广告".要么 "?" 在链接中.是否有Nokogiri函数可以执行此操作,可能返回所有图像的数组,我可以按照我想要的方式过滤它们?

Mad*_*sen 5

以下XPath应选择符合您所述标准的图像:

/html/body//img[@src[contains(.,'://') 
                     and not(contains(.,'ads.') 
                             or contains(.,'ad.') 
                             or contains(.,'?')
                            )
                     ]
                ][1]
Run Code Online (Sandbox Code Playgroud)

你可以像这样使用它:

doc.xpath("/html/body//img[@src[contains(.,'://') 
       and not(contains(.,'ads.') or contains(.,'ad.') or contains(.,'?'))]][1]")
Run Code Online (Sandbox Code Playgroud)