Zester
Zester is a library that makes it easier to develop Python clients for websites without APIs.
No lxml, no XPath, just javascript.
Let’s make a client library for Hacker News by saving the following code in a file named hnclient.py:
from zester import MultipleClient, Attribute
class HNClient(MultipleClient):
url = "http://news.ycombinator.com/"
title = Attribute(selector="$('.title a')", modifier="$(el).html()")
link = Attribute(selector="$('.title a')"), modifier="$(el).attr('href')")
points = Attribute(selector="$('.subtext span')", modifier="$(el).html().replace(' points', '')")
Now, let’s use the client we just made. Open a python shell:
>>> from hnclient import HNClient >>> client = HNClient() >>> stories = client.process() >>> stories[0] HNClientResponse(points=u'200', link=u'http://daltoncaldwell.com/what-twitter-could-have-been', title=u'What Twitter could have been') >>> print stories[0].title What Twitter could have been >>> print stories[0].link http://daltoncaldwell.com/what-twitter-could-have-been >>> print stories[0].points 56
We subclassed MultipleClient there because we were planning on returning multiple results. If we wanted to make a client for something like Weather.gov that returned a single result, we could do something like this:
from zester import SingleClient, Attribute
class WeatherClient(SingleClient):
url = "http://forecast.weather.gov/MapClick.php?lat={lat}&lon={lng}"
temperature = Attribute(selector="$('.myforecast-current-lrg').html()")
humidity = Attribute(selector="$('.current-conditions-detail li').contents()[1]")
heat_index = Attribute(selector="$('.current-conditions-detail li').contents()[11]")
def __init__(self, lat, lng, *args, **kwargs):
super(WeatherClient, self).__init__(*args, **kwargs)
self.url = self.url.format(lat=lat, lng=lng)
This also demonstrates how you can allow arguments to be taken:
>>> from weather_client import WeatherClient >>> client = WeatherClient(lat=40.7143528, lng=-74.0059731) >>> curr_weather = client.process() >>> curr_weather WeatherClientResponse(heat_index=u'82\xb0F (28\xb0C)', temperature=u'80\xb0F', humidity=u'58%') >>> print curr_weather.temperature 80°F >>> print curr_weather.humidity 58% >>> print curr_weather.heat_index 82°F (28°C)
Installation
Zester is dependant upon Ghost.py. You must install it before installing Zester. Ghost.py will also require the installation of either PyQt or PySide.
After Ghost.py is installed, to install zester:
$ pip install zester
Metadata
Release files for zester 0.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| zester-0.0.3.tar.gz | 3.2 kB | Without provenance | Details for zester-0.0.3.tar.gz |
Release files / zester-0.0.3.tar.gz
| Download URL | zester-0.0.3.tar.gz |
|---|---|
| Size | 3.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e1fc539ab9f0908440fbbaf6fd44961d5d770373ebd66e9597ef5417ee683b09
|
|
BLAKE2b-256 checksum How to use checksums |
5bb29f50c7ac13ee895399e2663ece2991ad6cd30edc3ba9ac371aee28b528dc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for zester 0.0.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.