V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
vitovan
V2EX  ›  分享创造

[Such-Cute] 网页 -> JSON 转换器,零编程。

  •  2
     
  •   vitovan · 2015-09-14 09:32:29 +08:00 · 3934 次点击
    这是一个创建于 3161 天前的主题,其中的信息可能已经有所发展或是发生改变。

    心急的朋友可以直接 F12 然后访问:
    http://sc.vitovan.com/get?uri=http://v2ex.com&selector=a&attrs=["text","href"]
    进阶使用可以参考:
    http://vitovan.github.io/such-cute/index-cn.html


    1 ) 你肯定写过爬虫

    我也写过。我的第一个爬虫是 Java 写的自动签到机,在公司上班的时候帮了我不少忙,每天都可以睡到自然醒而不用被扣工资。

    直到有一天老板来到了我们办公室,而那时候我还在家里的床上。

    2 ) 你肯定写过不止一个爬虫

    好巧,我也是。第二个爬虫是用来追踪姑娘在虾米上的听歌历史,一旦发现有新的条目,立马就把歌曲播放给我(后来发现虾米好像出的有一个什么「跟听」)。

    这个 是用 Python 写的,一直用的很好,直到有一天我受够了它播放给我的曲子。

    3 ) 你肯定不只写过两个爬虫

    我也是。我写的第三个爬虫花费了不到 10 个小时的时间,还带着一个地图界面。 把 V2EX 上的招聘帖子抽取过来分析一下然后转化成 JSON ,再分地域展示到高德地图上。

    感觉很叼的样子,然而并没有什么卵人用,每天的访问量小于 2 ,还要减去我的那个访问。

    4 ) 有没有觉得有些不对劲

    我感觉到了(广告要来了)。

    写个爬虫并不是很难,但也会花费你不少时间,最后可能还没个卵用。

    你还要选个语言 ,然后选个包(抽取网页啦、 DOM 解析啦),然后开始写。

    然后调试、测试然后天就黑了,是时候改变一下啦。

    5 ) 好了,下面是解决方案

    我创建了一个项目 cl-spider。现在它提供两个方法,主要是以 URICSS SELECTORS 以及 HTML ATTRIBUTES 为参数,然后返回给你想要的数据,比如:

    (cl-spider:get-data "https://news.ycombinator.com/" :selector "a" :attrs '("href" "text"))
    

    "然而这有个卵用,我不用 Lisp 。" - 杰克·派森王

    "太废物了, JSON 呢?" - 洛斯·爪哇娜

    6 ) 额,真正的解决方案

    由于现在用 Lisp 的人不多,我就又创建了一个项目叫 such-cute,它是 cl-spider 的一个网络接口。

    such-cute,你就可以不用触碰 Lisp 代码了,直接用 http 调用就可以返回 JSON 数据:

    http://localhost:5000/get?uri=https://news.ycombinator.com/&selector=a&attrs=["href","text"]

    你只需要按照指示搭建一个 such-cute 服务。

    "不 不 不 不 不,别想让我碰 Lisp 。" - 雷曼·菲普尔

    "这太愚蠢了!我还得弄个 Common Lisp 运行环境?" - 格斯塔夫·鲍斯尔

    "算了,我要点叉了。" - 西蒙·麦内格

    7 ) 算了,就当我什么也没说

    点击下面链接:

    http://sc.vitovan.com/get?uri=http://v2ex.com/&selector=a&attrs=["href","text"]

    然后看这个:http://vitovan.github.io/such-cute/index-cn.html

    第 1 条附言  ·  2015-09-14 11:25:27 +08:00
    类似工具:
    Yahoo Query Language (YQL ) https://developer.yahoo.com/yql/
    39 条回复    2015-09-29 06:50:43 +08:00
    mystryl
        1
    mystryl  
       2015-09-14 09:37:35 +08:00   ❤️ 2
    看错成 S-cute ,我相信我不是一个人。。。
    unique
        2
    unique  
       2015-09-14 09:38:35 +08:00
    @mystryl 求番号 2333
    vitovan
        3
    vitovan  
    OP
       2015-09-14 09:43:17 +08:00
    @unique 隐约觉得要歪楼了。
    oott123
        4
    oott123  
       2015-09-14 09:49:48 +08:00 via Android   ❤️ 2
    如果你没用过 YQL ,你应该试试。
    vitovan
        5
    vitovan  
    OP
       2015-09-14 09:50:40 +08:00
    @oott123 WTF.
    vitovan
        6
    vitovan  
    OP
       2015-09-14 09:54:23 +08:00
    @oott123 顿时觉得自己好愚蠢。
    faceair
        7
    faceair  
       2015-09-14 10:22:41 +08:00
    你也可以试试 pyspider
    oott123
        8
    oott123  
       2015-09-14 10:50:13 +08:00   ❤️ 1
    我试着点了一下
    sc.vitovan.com/get?uri=http://cachefly.cachefly.net/100mb.test/&selector=a&attrs=%5B%22href%22,%22text%22%5D
    然后你的服务器果断的 502 了…… 对不起哈……
    vitovan
        9
    vitovan  
    OP
       2015-09-14 10:57:12 +08:00
    @oott123 ......
    vitovan
        10
    vitovan  
    OP
       2015-09-14 11:00:25 +08:00
    @oott123 不至于啊,怎么回事儿呢。
    Tink
        11
    Tink  
       2015-09-14 11:34:08 +08:00   ❤️ 1
    好像还可以,省了剥离 dom 什么的
    21grams
        12
    21grams  
       2015-09-14 11:41:15 +08:00
    最后不还是要折腾 lisp 吗?
    vitovan
        13
    vitovan  
    OP
       2015-09-14 11:44:27 +08:00
    @21grams 搭了个个服务,不过不是很稳定。
    http://vitovan.github.io/such-cute/index-cn.html
    可以试试。
    laoyuan
        14
    laoyuan  
       2015-09-14 11:48:34 +08:00
    然而并没有什么卵人用。。。
    vitovan
        15
    vitovan  
    OP
       2015-09-14 11:54:55 +08:00
    @oott123 嘿!多谢啊!有没有什么好的避免的方法?
    oott123
        16
    oott123  
       2015-09-14 11:57:16 +08:00
    @vitovan 设置连接超时、下载超时;读取 x 字节后自动断开;读取到 HTTP 头后获取尺寸,超过一定尺寸则拒绝服务
    vitovan
        17
    vitovan  
    OP
       2015-09-14 12:10:28 +08:00
    @oott123 Thanks, 现在仅做了 5 秒超时处理,我再调整调整。
    ychongsaytc
        18
    ychongsaytc  
       2015-09-14 12:15:59 +08:00   ❤️ 1
    第一眼看到 s-cute 的自觉面壁。。
    Strikeactor
        19
    Strikeactor  
       2015-09-14 12:37:03 +08:00
    wfxiong1990
        20
    wfxiong1990  
       2015-09-14 14:42:37 +08:00
    问各路大神
    同样是 BBS ,水木清华 http://sc.vitovan.com/get?uri=http://m.newsmth.net//&selector=a&attrs=["text as title","href as uri"] 就解析正确

    但是 日月光华 http://sc.vitovan.com/get?uri=http://bbs.fudan.edu.cn/m/bbs/top10/&selector=a&attrs=["text as title","href as uri"] 就报错呢?
    vitovan
        21
    vitovan  
    OP
       2015-09-14 16:18:45 +08:00
    @oott123 已修复,多谢(好费劲的说,有些网页居然没有 Content-Length ,如 知乎首页)。
    vitovan
        22
    vitovan  
    OP
       2015-09-14 16:20:55 +08:00
    @wfxiong1990 哦,你是说乱码啊~
    日月光华的编码是 gb18030 ,这种编码还需要特殊处理,还未实现,稍稍等一下下哈。
    BikeMan
        23
    BikeMan  
       2015-09-15 20:06:07 +08:00
    这写作水平, 一看就是看了大量纯英的书籍, 写得这么欧美化, 读起来很爽, 你要不要考虑出本关于技术上的书籍?
    vitovan
        24
    vitovan  
    OP
       2015-09-15 20:16:00 +08:00
    @BikeMan 想啊,可不知道写啥,也不知道写了有没有市场撒。
    BikeMan
        25
    BikeMan  
       2015-09-16 11:25:07 +08:00   ❤️ 1
    @vitovan 针对有其他编程语言经验的人, 写一本易懂的 Common Lisp 学习教程, 为了更加实用, 把实战环境教程也写进去. 鉴于实体书难搞, 所以你得走电子书. 而且建议写英文版的电子书, 国内付费环境你懂滴, 而且用 Lisp 也很少. 接着专门为这书搞个官网, 然后标价. 就像 raywenderlich.com 这样售卖 IOS 教程电子书. 为了更加有趣, 书的内容也得有你平时用的什么插件,工具丫什么的. 800 页的(无凑字的), 标价 80 美刀, 我是会买的.
    vitovan
        26
    vitovan  
    OP
       2015-09-16 12:45:13 +08:00
    @BikeMan raywenderlich.com
    这是专业玩家啊。
    BikeMan
        27
    BikeMan  
       2015-09-16 15:53:00 +08:00   ❤️ 1
    @vitovan 那又怎么样呢, 一点点来就是啦. 谁能一口气成为那么专业的? 可以先从小技术玩起走, 比如写个 Chrome 插件的有趣教程, 用真实实例来讲解, 比如写个 youtube 视频下载插件, 写个 100 页左右的, 标价 10 几美刀就行. 你可以看到, 专门写 Chrome 插件的书籍少得可怜.

    写书, 就要写那些小众的, 或者现在很冷门, 为了绝对会火起来的东西. 大众的很难竞争, 比如 PHP 教程. 我看到你英语好到爆, 而且写得特别诙谐, 所以给了你这个 idea, 我本想自己干的, 但是英语实在是太烂了.
    BikeMan
        28
    BikeMan  
       2015-09-16 15:54:51 +08:00
    @vitovan 另外, 我有在 raywenderlich.com 上面花过接近 100 美刀. 因为写得好, 写得简单易懂.
    vitovan
        29
    vitovan  
    OP
       2015-09-16 17:18:22 +08:00 via Android
    @BikeMan 多谢,多谢。
    morefreeze
        30
    morefreeze  
       2015-09-18 12:01:13 +08:00   ❤️ 1
    我猜把 url 直接放地址栏里没转码之后会出问题,比如一个 http://foo.com/bar?selector=eve&selector=a
    haiyang416
        31
    haiyang416  
       2015-09-19 17:49:08 +08:00
    5 ) Eenough talk, show me the solution
    vitovan
        32
    vitovan  
    OP
       2015-09-19 18:08:35 +08:00
    @haiyang416 Should be "Enough talking" ? Sorry about that.
    haiyang416
        33
    haiyang416  
       2015-09-19 18:38:27 +08:00
    @vitovan 错别字
    vitovan
        34
    vitovan  
    OP
       2015-09-19 18:45:54 +08:00
    @haiyang416 嗯,发现了,多谢指正!
    front10
        35
    front10  
       2015-09-22 09:54:03 +08:00   ❤️ 1
    @vitovan 想法赞个
    另外,参数 url 麻烦转一下码,否则 url 再含有参数你咋整?

    @BikeMan
    feuvan
        36
    feuvan  
       2015-09-22 11:25:43 +08:00
    @mystryl hhh
    vitovan
        37
    vitovan  
    OP
       2015-09-22 14:32:37 +08:00
    @front10
    @morefreeze
    可以 POST 的。
    vitovan
        38
    vitovan  
    OP
       2015-09-29 06:49:32 +08:00
    @front10

    http://sc.vitovan.com/get?uri=https://news.ycombinator.com/user&selector=table[id=hnmain]>tr:nth-child(3)>td>table>tr&attrs=["text"]&params=["id as VitoVan"]

    新增了 URL 传参方法,文档已更新: http://vitovan.github.io/such-cute/

    请原谅之前的愚蠢与无知,以为 POST 可以解决问题。
    vitovan
        39
    vitovan  
    OP
       2015-09-29 06:50:43 +08:00
    @morefreeze

    http://sc.vitovan.com/get?uri=https://news.ycombinator.com/user&selector=table[id=hnmain]>tr:nth-child(3)>td>table>tr&attrs=["text"]&params=["id as VitoVan"]

    参见上一条回复。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3506 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 10:53 · PVG 18:53 · LAX 03:53 · JFK 06:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.