“语法错误,意外的 tIDENTIFIER,期待 $end"

问题描述:

我根据这个教程编写了这个脚本.

I put together this script based on this tutorial.

require 'nokogiri'
require 'open-uri'

url = "http://sfbay.craigslist.org/sby/jjj/"

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

puts data.at_css('.itempn').text
puts data.at_css('.itemcg').text

我不断收到此错误:

Macintosh:nokogiri rgrush$ ruby aaa.rb
aaa.rb:1: syntax error, unexpected tIDENTIFIER, expecting $end
url = "http://sf...
                      ^

有什么想法吗?可能是我的依赖项之一已过期?

Any ideas? Could it be that one of my dependencies is out of date?

很可能您在 URL 中有一个非 ASCII 字符.

most likely you have a non ASCII char in URL.

尝试添加

# encoding: UTF-8

作为aaa.rb

所以它看起来像:

# encoding: UTF-8
require 'nokogiri'
require 'open-uri'