Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.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 on rails Rake任务未在数据库中保存或创建新记录_Ruby On Rails_Ruby_Activerecord_Rake_Rakefile - Fatal编程技术网

Ruby on rails Rake任务未在数据库中保存或创建新记录

Ruby on rails Rake任务未在数据库中保存或创建新记录,ruby-on-rails,ruby,activerecord,rake,rakefile,Ruby On Rails,Ruby,Activerecord,Rake,Rakefile,我已经创建了一个ruby脚本,如果我从控制台运行它,它会执行得很好 该脚本从各种网站获取一些信息,并将其保存到我的数据库表中 但是,当我想将代码转换为rake任务时,代码仍然会运行,但不会保存任何新记录。我也没有从耙子上得到任何错误 # Add your own tasks in files placed in lib/tasks ending in .rake, # for example lib/tasks/capistrano.rake, and they will automatical

我已经创建了一个ruby脚本,如果我从控制台运行它,它会执行得很好

该脚本从各种网站获取一些信息,并将其保存到我的数据库表中

但是,当我想将代码转换为rake任务时,代码仍然会运行,但不会保存任何新记录。我也没有从耙子上得到任何错误

# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be           available to Rake.

require File.expand_path('../config/application', __FILE__)

Rails.application.load_tasks

require './crawler2.rb'
task :default => [:crawler]

task :crawler do

### ###

require 'rubygems'
require 'nokogiri'
require 'open-uri'

start = Time.now

$a = 0

sites = ["http://www.nytimes.com","http://www.news.com"]

for $a in 0..sites.size-1

url = sites[$a] 

$i = 75

$error = 0

avoid_these_links = ["/tv", "//www.facebook.com/"]

doc = Nokogiri::HTML(open(url))

    links = doc.css("a")
    hrefs = links.map {|link| link.attribute('href').to_s}.uniq.sort.delete_if {|href| href.empty?}.delete_if {|href| avoid_these_links.any? { |w| href =~ /#{w}/ }}.delete_if {|href| href.size < 10 }

#puts hrefs.length

#puts hrefs

for $i in 0..hrefs.length
    begin

        #puts hrefs[60] #for debugging)

    #file = open(url)
    #doc = Nokogiri::HTML(file) do

        if hrefs[$i].downcase().include? "http://"

            doc = Nokogiri::HTML(open(hrefs[$i]))

        else 

            doc = Nokogiri::HTML(open(url+hrefs[$i]))

        end 

        image = doc.at('meta[property="og:image"]')['content']
        title = doc.at('meta[property="og:title"]')['content']
        article_url = doc.at('meta[property="og:url"]')['content']
        description = doc.at('meta[property="og:description"]')['content']
        category = doc.at('meta[name="keywords"]')['content']

        newspaper_id = 1 


        puts "\n"
        puts $i
        #puts "Image: " + image
        #puts "Title: " + title
        #puts "Url: " + article_url
        #puts "Description: " + description
        puts "Catory: " + category

            Article.create({ 
            :headline => title, 
            :caption => description, 
            :thumbnail_url => image, 
            :category_id => 3, 
            :status => true, 
            :journalist_id => 2, 
            :newspaper_id => newspaper_id, 
            :from_crawler => true,
            :description => description,
            :original_url => article_url}) unless Article.exists?(original_url: article_url)

        $i +=1

        #puts $i #for debugging

        rescue
        #puts "Error here: " + url+hrefs[$i] if $i < hrefs.length
        $i +=1    # do_something_* again, with the next i
        $error +=1

    end 

end

puts "Page: " + url
puts "Articles: " + hrefs.length.to_s
puts "Errors: " + $error.to_s

$a +=1

end

finish = Time.now

diff = ((finish - start)/60).to_s

puts diff + " Minutes"


### ###


end
#将您自己的任务添加到lib/tasks中以.rake结尾的文件中,
#例如lib/tasks/capistrano.rake,它们将自动对rake可用。
需要文件。展开\u路径(“../config/application”,\u文件\u)
Rails.application.load_任务
需要“/crawler2.rb”
任务:默认=>[:爬虫]
任务:爬虫做什么
### ###
需要“rubygems”
需要“nokogiri”
需要“打开uri”
开始=时间。现在
$a=0
地点=[”http://www.nytimes.com","http://www.news.com"]
0..sites.size-1中的$a
url=站点[$a]
$i=75
$error=0
避免这些链接=[“/tv”,“www.facebook.com/”]
doc=Nokogiri::HTML(打开(url))
links=doc.css(“a”)
hrefs=links.map{| link | link.attribute('href')。to|s}.uniq.sort.delete|if{| href | href.empty?}。delete|if{| href |避免这些链接。任何?{w | href=~/#{w}/}。delete|if{href | href | href | href.size<10}
#把hrefs.length放进去
#放置hrefs
对于0..hrefs.length中的$i
开始
#放置hrefs[60]#用于调试)
#文件=打开(url)
#doc=Nokogiri::HTML(文件)do
如果hrefs[$i].downcase()。是否包括?“http://”
doc=Nokogiri::HTML(打开(hrefs[$i]))
其他的
doc=Nokogiri::HTML(打开(url+hrefs[$i]))
结束
image=doc.at('meta[property=“og:image”]')['content']
title=doc.at('meta[property=“og:title”]')['content']
article_url=doc.at('meta[property=“og:url”]”)['content']
description=doc.at('meta[property=“og:description”]')['content']
category=doc.at('meta[name=“keywords”]”)['content']
报纸id=1
放入“\n”
投入$i
#放置“图像:+图像
#放置“标题:”+标题
#放置“Url:+文章Url”
#放置“描述:”+描述
放置“Catory:+类别
第条.创建({
:headline=>title,
:caption=>说明,
:缩略图\u url=>图像,
:category_id=>3,
:status=>true,
:记者id=>2,
:报纸id=>报纸id,
:from_crawler=>true,
:description=>description,
:original\u url=>article\u url}),除非article.exists?(original\u url:article\u url)
$i+=1
#将$i#用于调试
营救
#如果$i

如果我将文件保存为crawler.rb并通过-->“load./crawler2.rb”在控制台中打开它,那么代码执行得很好。当我在rake任务中使用完全相同的代码时,我没有得到任何新记录。

我发现了问题所在

我需要删除:

require './crawler2.rb'
task :default => [:crawler]
并编辑以下内容:

task :crawler => :environment do
现在,在Heroku调度程序的帮助下,爬虫程序每十分钟运行一次:-)


谢谢大家的帮助,很抱歉格式不好。希望这个答案能对其他人有所帮助。

感觉这里缺少了一些东西。
任务:爬虫执行
从不以
结束
关闭。文章创建实际上是在任务内部吗?也许不是?谢谢你的意见,但恐怕不是这样。我用一些print/put语句进行了测试,这些语句也非常有效。就像代码跳过了.create部分一样。我不知道我使用Rake的方式是否错误,或者语法是否错误?语法是否错误。“do”需要和“end”在某个地方。请将您的代码格式化为ruby标准,并消除滚动。欢迎使用堆栈溢出。你可以改进你的问题。请阅读。当你的代码毫不费力地显示出你的问题时,你就是在向那些自愿帮助你的人表示尊重。