【发布时间】:2014-10-25 20:24:05
【问题描述】:
一段时间以来,我正在为电影制作一个简单的 clojure 项目,因此我试图解析来自特定网站的搜索结果,在我的例子中是 imdb.com。不确定我是否走在正确的轨道上,所以我希望有人能帮助我。
主页看起来很简单,带有文本字段,您可以在其中输入电影名称和名为“搜索”的提交按钮。我会尽量详细一点:
1.这是主页:
(defn view-input []
(view-layout
[:h2 "Find your Movie"]
[:body {:style "font: 14pt/16pt helvetica; background-color: #F2FB78; padding-top:100px; text-align: center" }
(form-to [:post "/"]
[:br]
[:br]
(text-field {:placeholder "Enter movie name" } :a) [:br]
(submit-button "Search")
)]
))
2.这些是我一直在使用的功能:
(defn create-flick-url [a]
(str "http://www.imdb.com/search/title?title=" a "&title_type=feature"
))
(defn flick-vec [categories a]
(vec (let [flick-url (create-flick-url a)
flick-names (print-flick-name-content flick-url)]
(mapper-gen4 flick-names
(get-image-content flick-url)
))) )
(defn view-output2 [categories a]
(view-layout
[:h2 "Search results"]
[:form {:method "post" :action "/"}
(interleave
(for [flick (flick-vec categories a)]
(label :title (:name flick)))
(for [flick-name (flick-vec categories a)]
[:br])
(for [flick-image (flick-vec categories a)]
[:img {:src (:image flick-image)}])
(for [flick (flick-vec categories a)]
[:br]))
]))
3.这是同一类中的 GET/POST 部分,我使用的是 view-output 和 view-output2 函数:
(defroutes main-routes
(GET "/" []
(view-input))
(POST "/" [categories a]
(view-output2 categories a))
4.另外,这些是之前使用的功能:
(defn print-flick-name-content
[url]
(vec (flatten (map :content (h3+table url)))))
(defn get-image-content
[url]
(vec (flatten (map #(re-find #"http.*jpg" %)
(map :style (map :attrs (h3+table2 url)))))))
(defn get-page
"Gets the html page from passed url"
[url]
(html/html-resource (java.net.URL. url)))
(defn h3+table
"Return seq of <h3> and table tags, where content of the <h3> tag meet defined condition"
[url]
(html/select (get-page url)
[:td (html/attr= :class "title") :h3 :a]))
(defn h3+table2
"Return seq of <h3> and table tags, where content of the <h3> tag meet defined condition"
[url]
(html/select (get-page url)
[:td (html/attr= :class "image")]))
5.这是最后一个,在另一个处理地图的类中定义的函数:
(defn mapper-gen4
[names images] (sort-by :name (map #(hash-map
:name %1 :image %2) names images)))
我知道这有点多,但是这样有人会看到问题出在哪里,到目前为止,搜索结果页面没有显示任何结果,也没有错误,只有带有 h2 搜索结果标题的空白页面。提前致谢!
【问题讨论】:
-
请参阅How to create a Minimal, Complete, and Verifiable example。正如目前所写的那样,这个问题需要潜在回答者的大量投资。
标签: parsing search map clojure imdb