Toapi 正式发布 - 再也不愁没有数据!

2017-12-26  本文已影响44人  Loophole

Toapi - 再也不愁没有数据!

Github: https://github.com/gaojiuli/toapi

进入 Toapi

你是否有这样的需求,有一个很好的 idea,却没有很好的数据来源,好不容易找到相关数据来源的网站,却发现没有 API (或许有但需要收费),那么,我可以告诉你,Toapi 为此提供了很好的解决方案 ,它可以:

它的最后效果

// http://127.0.0.1:5000/pic/?q=coffee

{
    "Pixabay": [
        {
            "img": "https://cdn.pixabay.com/photo/2017/06/21/05/28/coffee-2426110__340.png"
        },
        {
            "img": "/static/img/blank.gif"
        }
    ],
    "Pexels": [
        {
            "img": "https://images.pexels.com/photos/302899/pexels-photo-302899.jpeg?h=350&auto=compress&cs=tinysrgb"
        },
        {
            "img": "https://images.pexels.com/photos/34085/pexels-photo.jpg?h=350&auto=compress&cs=tinysrgb"
        }
    ]
}

Toapi 是一个用Python编写的开源项目,若你有特殊的功能需求,可以自己定制,我们为你提供完整的生态:

使用 Toapi

让我们看下面这个简单的例子 - 仅仅是演示 目标网站是hackernews

from toapi import XPath, Item, Api, Settings


class MySettings(Settings):
    web = {
        "with_ajax": True,
        "request_config": {},
        "headers": None
    }

api = Api('https://news.ycombinator.com', settings=MySettings)

class Post(Item):
    url = XPath('//a[@class="storylink"]/@href')
    title = XPath('//a[@class="storylink"]/text()')

    class Meta:
        source = XPath('//tr[@class="athing"]')
        route = {'/news?p=:page': '/news?p=:page'}

class Page(Item):
    next_page = XPath('//a[@class="morelink"]/@href')

    class Meta:
        source = None
        route = {'/news?p=:page': '/news?p=:page'}

    def clean_next_page(self, next_page):
        return "http://127.0.0.1:5000/" + next_page

api.register(Page)
api.register(Post)

api.serve()
# Visit http://127.0.0.1:5000/news?p=1

不到四十行代码,你已经拥有了hackernews的 API 服务,介绍下目前官方提供的模板:

最后

Toapi 不会停止进步,从简陋的第一版到现在基本功能完善的版本,一个月来我们有近 400 次 commits,其中辛苦不必多说。

我们诚恳地希望开发者们使用 Toapi 来构建服务,并提出你的宝贵意见,上手 Toapi 是一件极其简单的事情,我们希望看到 Toapi 能为开源社区带来一点贡献,能为你减少构建 API 的麻烦。

Toapi 开发团队(@gaojiuli, @howie6879, @wuqiangroy)

上一篇 下一篇

猜你喜欢

热点阅读