Parse Utilities (ParseUtils)
This package helps you extracting python dict from html/xml contents
Installation
pip install parse-utils
Usage
from parse_utils.page_parser import PageParser, ItemExtractor
html_data = """
<html>
<head><title>This is title</title></head>
<body>
<p id="header">This is header id</p>
<p id="header">This is header2</p>
<p class="content">This is content</p>
</body>
</html>
"""
html_rows_data = """
<html>
<head><title>This is title</title></head>
<body>
<ul id="contents">
<li><a href="/first"> First Item</a><p>Description of Item 1</p></li>
<li><a href="/Second"> Second Item</a><p>Description of Item 2</p></li>
<li><a href="/Second"> Second Item</a><p>Description of Item 3</p></li>
<li><a href="/Second"> First Item</a><p>Description of Item 4</p></li>
</ul>
<p class="content">This is content</p>
</body>
</html>
"""
json_data = {"name": "Yogendra", "address": {"country": "Nepal", "city": "Pokhara",}}
def test_html_parser():
"""
"""
config = {
"header": ['//p[@id="header"]/text()'],
"content": ['//p[@class="content"]'],
"description": ["//body"],
}
pparser = PageParser(html_data)
item = pparser.extract_dict(config)
item2 = pparser.extract_dict(config, is_list=True)
print(item2["body"])
item3 = pparser.extract_dict(config, linebreaks=True)
print(item3["body"])
print(item)
def test_json_parser():
"""
"""
config = {
"header": ["name"],
"city": ["address", "city"],
}
jparser = PageParser(json_data, selector=True)
item = jparser.extract_dict_from_json(config)
print(item)
def test_items_parser():
"""
"""
config = {
"results": "//ul/li",
"fields": {
"title": ["./a/text()"],
"description": ["./p"],
"link": ["./a/@href"],
},
}
for item in ItemExtractor.extract_items(
config["results"], config["fields"], html_rows_data
):
print(item)
def test_items_parser_with_seed():
"""
"""
seed_dict = {'default_key': 'default_value'}
config = {
"results": "//ul/li",
"fields": {
"title": ["./a/text()"],
"description": ["./p"],
"link": ["./a/@href"],
},
}
for item in ItemExtractor.extract_items(
config["results"], config["fields"], html_rows_data, item=seed_dict
):
print(item)
def test_items_parser_with_results():
"""
"""
seed_dict = {'default': 'default_list'}
config = {
"results": ["//apple/ball", "//ul/li"],
"fields": {
"title": ["./a/text()"],
"description": ["./p"],
"link": ["./a/@href"],
},
}
for item in ItemExtractor.extract_items(
config["results"], config["fields"], html_rows_data, item=seed_dict
):
print(item)
if __name__ == "__main__":
test_html_parser()
test_json_parser()
test_items_parser()
test_items_parser_with_seed()
test_items_parser_with_results()
Output will be:
['This is header id\n This is header2\n This is content']
This is header id
This is header2
This is content
{'header': 'This is header id', 'content': 'This is content', 'body': 'This is header id\n This is header2\n This is content'}
{'header': 'Yogendra', 'city': 'Pokhara'}
{'title': 'First Item', 'description': 'Description of Item 1', 'link': '/first'}
{'title': 'Second Item', 'description': 'Description of Item 2', 'link': '/Second'}
{'title': 'Second Item', 'description': 'Description of Item 3', 'link': '/Second'}
{'title': 'First Item', 'description': 'Description of Item 4', 'link': '/Second'}
Release files for parse-utils 1.3.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| parse_utils-1.3.6-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Release files / parse_utils-1.3.6-py2.py3-none-any.whl
| Download URL | parse_utils-1.3.6-py2.py3-none-any.whl |
|---|---|
| Size | 4.5 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
c631944b15bc05e4332e5d5592c90e2b827a06c4f5a2bd753ff2a27e8372e4e7
|
|
BLAKE2b-256 checksum How to use checksums |
e63b4a10a22ee04bae35be0c127ccd532a9c67772bd911433e211154246c8abc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.0.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
|