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 未保存Xpath内容_Ruby_Xpath_Nokogiri_Scraper - Fatal编程技术网

Ruby 未保存Xpath内容

Ruby 未保存Xpath内容,ruby,xpath,nokogiri,scraper,Ruby,Xpath,Nokogiri,Scraper,这可能只是我还没有发现的代码中的一个愚蠢错误,但它已经花费了我相当长的时间:当使用nokogiri和xpath解析网站,并试图将xpath的内容保存到一个.csv文件时,csv文件的单元格是空的 基本上,xpath的内容返回为空,或者我的代码没有正确读取网站 这就是我正在做的: require 'open-uri' require 'nokogiri' require 'csv' CSV.open("neverend.csv", "w") do |csv| csv << ["k

这可能只是我还没有发现的代码中的一个愚蠢错误,但它已经花费了我相当长的时间:当使用nokogiri和xpath解析网站,并试图将xpath的内容保存到一个.csv文件时,csv文件的单元格是空的

基本上,xpath的内容返回为空,或者我的代码没有正确读取网站

这就是我正在做的:

require 'open-uri'
 require 'nokogiri'
 require 'csv'

CSV.open("neverend.csv", "w") do |csv|
csv << ["kuk","date","name"]

#first, open the urls from a document. The urls are correct.
File.foreach("neverendurls.txt") do |line|    

#second, the loop for each url
searchablefile = Nokogiri::HTML(open(line))

#third, the xpaths. These work when I try them on the website.
kuk = searchablefile.at_xpath("(//tbody/tr/td[contains(@style,'60px')])[1]")
date = searchablefile.at_xpath("(//tbody/tr/td[contains(@style,'60px')])[1]/following-sibling::*[1]")
name = searchablefile.at_xpath("(//tbody/tr/td[contains(@style, '60px')])[1]/following-sibling::*[2]")

#fourth, saving the xpaths
csv <<  [kuk,date,name]

end 
end
需要“打开uri”
需要“nokogiri”
需要“csv”
CSV.open(“neverend.CSV”,“w”)do | CSV|

csv从你发布的内容无法判断,但让我们用css清理一下这个棘手的问题:

kuk  = searchablefile.at 'td[style*=60px]'
date = searchablefile.at 'td[style*=60px] + *'
name = searchablefile.at 'td[style*=60px] + * + *'
我想我有答案了。。(1) 不信任浏览器xpath检查。(2) 小心,不要使用它!