Declarative hypertext client
Project description
Declarative hypertext client
Installation
pip install peppertext
Overview
from peppertext import Hypertext, SimpleURLField, resolve, register, selector
@register
class GoogleBlogPage(Hypertext):
url = SimpleURLField(
"https://googleblog.blogspot.kr/{year}/{month}/{title}.html"
)
title = selector.find(".title[itemprop=name]").text()
body = selector.find(".post-body").text()
It resolves given headers, url and query string to hypertext object.
>>> p = resolve("https://googleblog.blogspot.kr/2015/11/google-gobble-thanksgiving-trends-on.html")
>>> p
<GoogleBlogPage at 0x108a4d1f0 >
>>> p.fetch()
>>> p['title']
'Google gobble: Thanksgiving trends on Search'
>>> p['body']
'In just a few hours, people across the U.S. will be settling...'
You can create GoogleBlogPage object with profile variables which are declared as fields in GoogleBlogPage class.
>>> p = GoogleBlogPage(
... year="2015",
... month="11",
... title="google-gobble-thanksgiving-trends-on"
... )
>>> p.fetch()
>>> p['title']
'Google gobble: Thanksgiving trends on Search'
Selectors
class GoogleBlogPage(Hypertext):
# ...
title = selector.find(".title[itemprop=name]").text()
# ...
Selectors process a document which is returned from server as response. In the GoogleBlogPage example above, title selector parses document and find an element specified with “.title[itemprop=name]” css selector. You can access the value title with subscribing the GoogleBlogPage object with selector name.
document = pq("""<div>
<a href="http://example.com">Link1</a>
<a href="http://example.com/dahokan">Link2</a>
<a href="http://example.com/manoha">Link3</a>
</div>""")
find_selector = selector.find('a')
selected_els = find_selector.select(document)
self.assertEqual( [pq(el).attr["href"] for el in selected_els],
[
"http://example.com",
"http://example.com/dahokan",
"http://example.com/manoha"
]
)
find
Select html elements which match to given css selector string.
attribute
Get an element’s attribute value with given attribute name.
text
Select the html element’s inner text value.
at
Get an item on index
sub
sub_selector = selector.sub(pattern="\d+", repl="")
Do regex substitution.
cast
int_cast_selector = selector.cast(int)
Pass the data to the function given as a parameter.
Compatibility
Peppertext supports Python 2.7 and 3.
Features in developing
Interface for parse error handling
Polymorphic access to page selectors
Interface to resolve and traverse links in a page
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file peppertext-0.1.3.tar.gz
.
File metadata
- Download URL: peppertext-0.1.3.tar.gz
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5361a56694f8de9769df15020386d6beada1181a8cec6a05b16651bd00faf82 |
|
MD5 | 4f5a27c5f8524f0e6fec9680a191f2b6 |
|
BLAKE2b-256 | 3fdfad332f43257427a819576c844e233d5dbf244491cfd06e065d878ac57943 |