Created
November 2, 2018 13:48
-
-
Save traverseda/078f68a44205b136dbc032d4041e5ffd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from camel import CamelRegistry, Camel | |
| from bs4 import BeautifulSoup | |
| import bs4 | |
| html_types = CamelRegistry() | |
| @html_types.dumper(BeautifulSoup, 'xml', version=1) | |
| @html_types.dumper(bs4.element.Tag, 'xml', version=1) | |
| def _dump_tag(tag): | |
| out = dict( | |
| tagName= str(tag.name), | |
| children = list(tag.children), | |
| **{k:v for k,v in tag.attrs.items() if v} | |
| ) | |
| return {k:v for k,v in out.items() if v} | |
| @html_types.dumper(bs4.element.NavigableString, 'NavigableString', version=1) | |
| @html_types.dumper(bs4.element.CharsetMetaAttributeValue, 'CharsetMetaAttributeValue', version=1) | |
| @html_types.dumper(bs4.element.ContentMetaAttributeValue, 'ContentMetaAttributeValue', version=1) | |
| @html_types.dumper(bs4.element.ProcessingInstruction, 'ProcessingInstruction', version=1) | |
| @html_types.dumper(bs4.element.Doctype, 'Doctype', version=1) | |
| @html_types.dumper(bs4.element.Declaration, 'Declaration', version=1) | |
| @html_types.dumper(bs4.element.CData, 'CData', version=1) | |
| @html_types.dumper(bs4.element.Comment, 'Comment', version=1) | |
| def dump_ContentString(string): | |
| return str(string).strip() | |
| @html_types.loader('xml', version=1) | |
| @html_types.loader('NavigableString', version=1) | |
| @html_types.loader('CharsetMetaAttributeValue', version=1) | |
| @html_types.loader('ContentMetaAttributeValue', version=1) | |
| @html_types.loader('ProcessingInstruction', version=1) | |
| @html_types.loader('Doctype', version=1) | |
| @html_types.loader('Declaration', version=1) | |
| @html_types.loader('CData', version=1) | |
| @html_types.loader('Comment', version=1) | |
| def _load_table(data, version): | |
| return data | |
| import requests | |
| soup = BeautifulSoup(requests.get("https://forums.sufficientvelocity.com/threads/marked-for-death-a-rational-naruto-quest.24481/page-4574").text, 'html5lib') | |
| foo = Camel([html_types,]).dump(soup) | |
| #foo = Camel([html_types,]).load(open('./test.yaml','r').read()) | |
| print(foo) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment