它潜伏了很久了 昨天才被我发现
#1 楼 @fresh_fish X,被你这么一说好害怕呢
#16 楼 @hellojane96 链接呢
5. No authentication or cookies will be sent with the request
In order to prevent misuse of the user’s ambient authority (e.g. cookies, HTTP credentials, client certificates, etc), the request will be stripped of cookies and credentials and will ignore any authentication challenges or Set-Cookie directives in the HTTP response. XDomainRequests will not be sent on previously-authenticated connections, because some Windows authentication protocols (e.g. NTLM/Kerberos) are per-connection-based rather than per-request-based.
Sites that wish to perform authentication of the user for cross-origin requests can use explicit methods (e.g. tokens in the POST body or URL) to pass this authentication information without risking the user’s ambient authority.
这一点就不太适合吧 ---update-- 没看明白,这东西是在认证的那个请求不发送 cookie 还是所有请求都不能发送?
#6 楼 @lionzixuanyuan 好看吧
先打印 str_got_at.encoding 然后打印 str_got_at.bytes.to_a.inspect 然后用 DateTime.parse(Iconv.iconv("UTF-8", "GBK", str_got_at))
中间少个空格吧 应该是2012-12-14 15:05:52
好吧,这样!
class StockspiderWorker
include Sidekiq::Worker
def perform
Stock.all.each do |stock|
@s = Net::HTTP.get(URI.parse("http://hq.sinajs.cn/list=" + stock[:market] + stock[:code]))
@s = @s[@s.index("\"")+1, @s.rindex("\"")-1]
@records = @s.split(",")
str_got_at = "#{@records[30]}#{@records[31]}"
Rails.logger.info(str_got_at)
Rails.logger.info "*" * 100
got_at = DateTime.parse(str_got_at)
stock.update_attributes(:name => Iconv.iconv("UTF-8", "GBK", @records[0]).first.to_s,
:volume => @records[8], :obv => @records[9],
:op => @records[1], :ycp => @records[2],
:cp => @records[3], :tp => @records[4],
:lp => @records[5],
:got_at => got_at)
end
end
end
#34 楼 @bhuztez 找到 speaker deck 用的那个 gem 了:https://github.com/jonmagic/grim 可以从 pdf 获取文字和抽取成图片。
class StockspiderWorker
include Sidekiq::Worker
def perform
Stock.all.each do |stock|
@s = Net::HTTP.get(URI.parse("http://hq.sinajs.cn/list=" + stock[:market] + stock[:code]))
@s = @s[@s.index("\"")+1, @s.rindex("\"")-1]
@records = @s.split(",")
got_at = DateTime.parse("#{@records[30]}#{@records[31]}")
Rails.logger.info(got_at)
Rails.logger.info "*" * 100
stock.update_attributes(:name => Iconv.iconv("UTF-8", "GBK", @records[0]).first.to_s,
:volume => @records[8], :obv => @records[9],
:op => @records[1], :ycp => @records[2],
:cp => @records[3], :tp => @records[4],
:lp => @records[5],
:got_at => got_at)
end
end
end
这样试试,看看日志输出
你 worker 里到代码是怎么写的? /Users/stephen/Gupiao/app/workers/stockspider_worker.rb:17 这里出错那个方法
1.9.3p327 :010 > DateTime.parse(" ")
ArgumentError: invalid date
from (irb):10:in `parse'
from (irb):10
from /home/hooopo/.rvm/rubies/ruby-1.9.3-p327/bin/irb:16:in `<main>'
1.9.3p327 :011 > Time.parse " "
ArgumentError: no time information in " "
from /home/hooopo/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/time.rb:267:in `parse'
from (irb):11
from /home/hooopo/.rvm/rubies/ruby-1.9.3-p327/bin/irb:16:in `<main>'
1.9.3p327 :012 >
根据你的错误信息,你这两个错误都是因为传的是空字符串导致的。
直接用Time.parse