A library letting any web site provide APIs.
- Converting static html to api.
- Serving as an api server.
- Supporting both XPath selector and Regex.
- Supporting custom request header.
pip install toapi
pip install git+https://github.com/gaojiuli/toapi/
from toapi import XPath, Item, Api
api = Api('https://news.ycombinator.com/')
class Post(Item):
url = XPath('//a[@class="storylink"][1]/@href')
title = XPath('//a[@class="storylink"][1]/text()')
class Meta:
source = XPath('//tr[@class="athing"]')
route = '/'
api.register(Post)
print(api.parse('/'))
api.serve()
Phantomjs
is required. Runphantomjs -v
to check.- If you use Ubuntu. Run
sudo apt install phantomjs
to install. - If you use MacOS. Run
brew install phantomjs
to install.
from toapi import XPath, Item, Api
api = Api('https://news.ycombinator.com/', with_ajax=True) # This meas use selenium to load the page source.
class Post(Item):
url = XPath('//a[@class="storylink"][1]/@href')
title = XPath('//a[@class="storylink"][1]/text()')
class Meta:
source = XPath('//tr[@class="athing"]')
route = '/'
api.register(Post)
print(api.parse('/'))
api.serve()
Then, You get your api server. Powered by flask.
- Open issue.
- Pull Request.
Apache-2.0