首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在enlive中解析来自httpclient结果

如何在enlive中解析来自httpclient结果
EN

Stack Overflow用户
提问于 2016-01-24 20:50:01
回答 1查看 132关注 0票数 2

在下面的链接https://github.com/swannodette/enlive-tutorial/blob/master/src/tutorial/scrape1.clj

它展示了如何从URL解析页面,但是我需要使用sock5代理,并且我不知道如何在enlive中使用代理,但是我知道如何在httpclient中使用proxy,但是如何解析来自httpclient的结果,我有以下代码,但最后一行显示的结果为空

代码语言:javascript
复制
    (:require [clojure.set :as set]
                [clj-http.client :as client]
                [clj-http.conn-mgr :as conn-mgr]
                [clj-time.core :as time]
                [jsoup.soup :as soup]
                [clj-time.coerce :as tc]
                [net.cgrand.enlive-html :as html]
                )     
     (def a (client/get "https://news.ycombinator.com/"
                             {:connection-manager (conn-mgr/make-socks-proxied-conn-manager "127.0.0.1" 9150)
                              :socket-timeout 10000 :conn-timeout 10000
                              :client-params {"http.useragent" "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.20 (KHTML, like Gecko) Chrome/11.0.672.2 Safari/534.20"}}))
(def b (html/html-resource a))
(html/select b [:td.title :a])
EN

回答 1

Stack Overflow用户

发布于 2016-01-25 00:00:18

当使用enlive时,URL执行从html-resource的获取,然后将其转换为它可以解析的数据结构。似乎当你向它传递一个已经完成的请求时,它只是返回请求,而不是抛出一个错误。

无论哪种方式,您想要的函数都是html-snippet,并且您需要将它作为请求的主体传递给它。如下所示:

代码语言:javascript
复制
;; Does not matter if you are using a connection manager or not as long as
;; its returning a result with a body
(def req (client/get "https://news.ycombinator.com/"))

(def body (:body req))
(def nodes (html/html-snippet body))
(html/select nodes [:td.title :a])

;; Or you can put it all together like this

(-> req
    :body 
    html/html-snippet
    (html/select [:td.title :a])))
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34975999

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档