例外:未绑定不能转换为 clojure.lang.Named,正在编译
Exceptions: Unbound cannot be cast to clojure.lang.Named,compiling
我正在尝试使用 clj-http/client 检索网页。看起来它有点工作,但有时我会收到这种异常
Exception in thread "main" java.lang.ClassCastException: clojure.lang.Var$Unbound cannot be cast to clojure.lang.Named,compiling:(/tmp/form-init8570082100332402765.clj:1:72)
这是一个简单的函数,它从数据库中检索 url(来自 jdbc/query :row-fn),请求 url 的内容并将其写入数据库.代理数据是随机的,仅供参考。
(defn get-source
"get content of an url"
[row]
(def my-proxy "72.159.148.20")
(def my-port 10000)
(def my-url (:url row))
(def h {"User-Agent" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/34.0"})
(try
(def my-body (:body (client/get my-url {:proxy-host my-proxy :proxy-port my-port :follow-redirects false :headers h :conn-timeout 100000})))
(catch clojure.lang.ExceptionInfo e
(prn "caught" e))
)
(write-data-to-db my-url my-body))
行的值来自简单的数据库查询:
(defn -main
"I don't do a whole lot ... yet."
[& args]
(def db-spec
{
:subprotocol "mysql"
:subname "//localhost:3306/a"
:user "user"
:password "pass"})
((jdbc/query db-spec ["SELECT url FROM main where html is null and url is not null limit 2"] :row-fn get-source)))
感谢中肯的批评,我已经修正了我的例子,使其更正确。
(defn get-source
"get list of urls to grab in"
[row]
(let [proxy "107.161.31.220" port 8080 url (:url row)
h {"User-Agent" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/34.0"}]
(:status (client/get url {:proxy-host proxy :proxy-port port :follow-redirects false :headers h :conn-timeout 100000}))))
我希望这样会好很多,我再也不能得到那个异常了,所以它可能被埋在我最初带来的混乱中。
我认为在函数或“块”中定义某些东西在 Clojure 中不是惯用的(或者它只是非声明性的)。 def 声明全局变量。
来自http://clojure.org/special_forms:
(def symbol init?)
Creates and interns or locates a global var with the name of symbol and a namespace of the value of the current namespace. <...> def yields the var itself (not its value).
我想您必须将 (write-data-to-db) 放在 try-block 中并使用 let 而不是 def。现在您正在尝试访问可能会失败的东西的价值。
我正在尝试使用 clj-http/client 检索网页。看起来它有点工作,但有时我会收到这种异常
Exception in thread "main" java.lang.ClassCastException: clojure.lang.Var$Unbound cannot be cast to clojure.lang.Named,compiling:(/tmp/form-init8570082100332402765.clj:1:72)
这是一个简单的函数,它从数据库中检索 url(来自 jdbc/query :row-fn),请求 url 的内容并将其写入数据库.代理数据是随机的,仅供参考。
(defn get-source
"get content of an url"
[row]
(def my-proxy "72.159.148.20")
(def my-port 10000)
(def my-url (:url row))
(def h {"User-Agent" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/34.0"})
(try
(def my-body (:body (client/get my-url {:proxy-host my-proxy :proxy-port my-port :follow-redirects false :headers h :conn-timeout 100000})))
(catch clojure.lang.ExceptionInfo e
(prn "caught" e))
)
(write-data-to-db my-url my-body))
行的值来自简单的数据库查询:
(defn -main
"I don't do a whole lot ... yet."
[& args]
(def db-spec
{
:subprotocol "mysql"
:subname "//localhost:3306/a"
:user "user"
:password "pass"})
((jdbc/query db-spec ["SELECT url FROM main where html is null and url is not null limit 2"] :row-fn get-source)))
感谢中肯的批评,我已经修正了我的例子,使其更正确。
(defn get-source
"get list of urls to grab in"
[row]
(let [proxy "107.161.31.220" port 8080 url (:url row)
h {"User-Agent" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/34.0"}]
(:status (client/get url {:proxy-host proxy :proxy-port port :follow-redirects false :headers h :conn-timeout 100000}))))
我希望这样会好很多,我再也不能得到那个异常了,所以它可能被埋在我最初带来的混乱中。
我认为在函数或“块”中定义某些东西在 Clojure 中不是惯用的(或者它只是非声明性的)。 def 声明全局变量。
来自http://clojure.org/special_forms:
(def symbol init?)
Creates and interns or locates a global var with the name of symbol and a namespace of the value of the current namespace. <...> def yields the var itself (not its value).
我想您必须将 (write-data-to-db) 放在 try-block 中并使用 let 而不是 def。现在您正在尝试访问可能会失败的东西的价值。