Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html nokogiri多个css类_Html_Css_Xpath_Parsing_Nokogiri - Fatal编程技术网

Html nokogiri多个css类

Html nokogiri多个css类,html,css,xpath,parsing,nokogiri,Html,Css,Xpath,Parsing,Nokogiri,如何选择一个包含两个类的html元素 例如,如何在HTML文档中选择下面的元素(假设它有两个css类)class='class1 class='class2' 我尝试使用以下方法: doc.xpath(“//p[@class~='class1 class2']”) doc.xpath(//p[@class~='class1'].[@class~='class2']) doc.xpath(//p[@class~='class1',@class~='class2']) doc.xpath(//p[

如何选择一个包含两个类的html元素

例如,如何在HTML文档中选择下面的元素
(假设它有两个css类)
class='class1 class='class2'

我尝试使用以下方法:

  • doc.xpath(“//p[@class~='class1 class2']”)
  • doc.xpath(//p[@class~='class1'].[@class~='class2'])
  • doc.xpath(//p[@class~='class1',@class~='class2'])
  • doc.xpath(//p[contains(concat(“”,@class,”),'class1')&&contains(concat(“”,@class,”),'class2'))”
但是没有成功


提前感谢

最后我找到了使用nokogiri(libxml)搜索多个css类的正确方法:

它不是完美的,因为如果
包含
class10
class20
之类的类,元素将被选中,但现在它已经足够满足我的需要了。如果你有更多的建议,欢迎

更新 以下是仅使用css解决此问题的更好方法:

doc.css('p.class1.class2')

多亏了Aaron Patterson:-)

试试
//p[@class~='class1'][@class~='class2']
,也许吧。对于其他人来说,当一个类只有数字(.2011)时,.css选择器不起作用。当解析为css时,2011是一个有效的类,您只需在定义样式时执行。\32 011{}。
doc.css('p.class1.class2')