Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.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
Ruby I';我试图提取html页面上的每个a href链接,以便使用nokogiri和xpath进行评估_Ruby_Xpath_Nokogiri - Fatal编程技术网

Ruby I';我试图提取html页面上的每个a href链接,以便使用nokogiri和xpath进行评估

Ruby I';我试图提取html页面上的每个a href链接,以便使用nokogiri和xpath进行评估,ruby,xpath,nokogiri,Ruby,Xpath,Nokogiri,我试图提取html页面上的每个a href链接,以便使用nokogiri和xpath进行评估。到目前为止,我所拥有的似乎只是把页面标题拉出来。我对链接标题不感兴趣,只对指向的URL感兴趣 以下是我所拥有的: doc = Nokogiri::HTML(open("http://www.cnn.com")) doc.xpath('//a').each do |node| puts node.text end 有谁能指导我如何纠正这一点,以便我提取实际的href而不是文本本身吗?您的XPATH/

我试图提取html页面上的每个a href链接,以便使用nokogiri和xpath进行评估。到目前为止,我所拥有的似乎只是把页面标题拉出来。我对链接标题不感兴趣,只对指向的URL感兴趣

以下是我所拥有的:

doc = Nokogiri::HTML(open("http://www.cnn.com"))
doc.xpath('//a').each do |node|
  puts node.text
end

有谁能指导我如何纠正这一点,以便我提取实际的href而不是文本本身吗?

您的XPATH//a正在提取所有元素。其中包括文本内容。您可以使用@attrname访问属性。比如说

//a/@href
将为您获取文档中每个a的href