Skip to content

Commit 96bf6b3

Browse files
committed
Merge remote-tracking branch 'upstream/master' into 53
2 parents 96c77af + d78167c commit 96bf6b3

26 files changed

+22369
-113
lines changed

HISTORY.rst

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
History
33
=======
44

5+
v0.10.0 (2020-09-01)
6+
--------------------
7+
8+
* support open graph arrays via ``with_og_array=True`` (PR #138)
9+
* support "expanded" Open Graph metadata based on og:type (PR #140)
10+
* parse JSON with JS comments for json-ld (PR #137)
11+
* preserve order for duplicated properties for RDFa (PR #139)
12+
* improve microdata parser performance with large number of items (PR #148)
13+
* spelling fixes (PR #145)
14+
515
v0.9.0 (2020-04-20)
616
-------------------
717

@@ -80,7 +90,7 @@ v0.5.0 (2018-06-08)
8090
html nodes.
8191
* ``base_url`` substitutes ``url`` in ``MicroformatExtractor``, ``JsonLdExtractor``,
8292
``OpenGraphExtractor``, ``RDFaExtractor`` and ``MicrodataExtractor``
83-
* individual extractors accpet ``base_url`` instead of ``url``, unused keyword
93+
* individual extractors accept ``base_url`` instead of ``url``, unused keyword
8494
arguments are removed.
8595
* In ``w3microdata.extract_items`` ``items_seen`` and ``url`` are no longer
8696
class variables but are passed as arguments.
@@ -152,7 +162,7 @@ v0.2.0 (2016-09-26)
152162
-------------------
153163

154164
* Web service response content-type set to 'application/json'
155-
* Web service Python 3 compatiblity
165+
* Web service Python 3 compatibility
156166
* Code coverage reports
157167
* Fix extraction of ``<object>`` "data" URL with microdata
158168
* Handle textContent mixed with ``<script>`` and ``<style>`` tags

README.rst

Lines changed: 91 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Currently, *extruct* supports:
2424
- `Microformat`_ via `mf2py`_
2525
- `Facebook's Open Graph`_
2626
- (experimental) `RDFa`_ via `rdflib`_
27+
- `Dublin Core Metadata (DC-HTML-2003)`_
2728

2829
.. _W3C's HTML Microdata: http://www.w3.org/TR/microdata/
2930
.. _embedded JSON-LD: http://www.w3.org/TR/json-ld/#embedding-json-ld-in-html-documents
@@ -32,6 +33,7 @@ Currently, *extruct* supports:
3233
.. _Microformat: http://microformats.org/wiki/Main_Page
3334
.. _mf2py: https://github.com/microformats/mf2py
3435
.. _Facebook's Open Graph: http://ogp.me/
36+
.. _Dublin Core Metadata (DC-HTML-2003): https://www.dublincore.org/specifications/dublin-core/dcq-html/2003-11-30/
3537

3638
The microdata algorithm is a revisit of `this Scrapinghub blog post`_ showing how to use EXSLT extensions.
3739

@@ -71,7 +73,17 @@ First fetch the HTML using python-requests and then feed the response body to ``
7173
>>> data = extruct.extract(r.text, base_url=base_url)
7274
>>>
7375
>>> pp.pprint(data)
74-
{ 'json-ld': [ { '@context': 'https://schema.org',
76+
{ 'dublincore': [ { 'elements': [ { 'URI': 'http://purl.org/dc/elements/1.1/description',
77+
'content': 'What is Open Graph Protocol '
78+
'and why you need it? Learn to '
79+
'implement Open Graph Protocol '
80+
'for Facebook on your website. '
81+
'Open Graph Protocol Meta Tags.',
82+
'name': 'description'}],
83+
'namespaces': {},
84+
'terms': []}],
85+
86+
'json-ld': [ { '@context': 'https://schema.org',
7587
'@id': '#organization',
7688
'@type': 'Organization',
7789
'logo': 'https://www.optimizesmart.com/wp-content/uploads/2016/03/optimize-smart-Twitter-logo.jpg',
@@ -163,7 +175,7 @@ First fetch the HTML using python-requests and then feed the response body to ``
163175

164176
Select syntaxes
165177
+++++++++++++++
166-
It is possible to select which syntaxes to extract by passing a list with the desired ones to extract. Valid values: 'microdata', 'json-ld', 'opengraph', 'microformat', 'rdfa'. If no list is passed all syntaxes will be extracted and returned::
178+
It is possible to select which syntaxes to extract by passing a list with the desired ones to extract. Valid values: 'microdata', 'json-ld', 'opengraph', 'microformat', 'rdfa' and 'dublincore'. If no list is passed all syntaxes will be extracted and returned::
167179

168180
>>> r = requests.get('http://www.songkick.com/artists/236156-elysian-fields')
169181
>>> base_url = get_base_url(r.text, r.url)
@@ -207,9 +219,9 @@ It is possible to select which syntaxes to extract by passing a list with the de
207219

208220
Uniform
209221
+++++++
210-
Another option is to uniform the output of microformat, opengraph, microdata and json-ld syntaxes to the following structure: ::
222+
Another option is to uniform the output of microformat, opengraph, microdata, dublincore and json-ld syntaxes to the following structure: ::
211223

212-
{'@context': 'http://example.com',
224+
{'@context': 'http://example.com',
213225
'@type': 'example_type',
214226
/* All other the properties in keys here */
215227
}
@@ -584,6 +596,80 @@ Microformat extraction
584596
}
585597
}]
586598

599+
DublinCore extraction
600+
++++++++++++++++++++++++++++++
601+
::
602+
603+
>>> import pprint
604+
>>> pp = pprint.PrettyPrinter(indent=2)
605+
>>> from extruct.dublincore import DublinCoreExtractor
606+
>>> html = '''<head profile="http://dublincore.org/documents/dcq-html/">
607+
... <title>Expressing Dublin Core in HTML/XHTML meta and link elements</title>
608+
... <link rel="schema.DC" href="http://purl.org/dc/elements/1.1/" />
609+
... <link rel="schema.DCTERMS" href="http://purl.org/dc/terms/" />
610+
...
611+
...
612+
... <meta name="DC.title" lang="en" content="Expressing Dublin Core
613+
... in HTML/XHTML meta and link elements" />
614+
... <meta name="DC.creator" content="Andy Powell, UKOLN, University of Bath" />
615+
... <meta name="DCTERMS.issued" scheme="DCTERMS.W3CDTF" content="2003-11-01" />
616+
... <meta name="DC.identifier" scheme="DCTERMS.URI"
617+
... content="http://dublincore.org/documents/dcq-html/" />
618+
... <link rel="DCTERMS.replaces" hreflang="en"
619+
... href="http://dublincore.org/documents/2000/08/15/dcq-html/" />
620+
... <meta name="DCTERMS.abstract" content="This document describes how
621+
... qualified Dublin Core metadata can be encoded
622+
... in HTML/XHTML &lt;meta&gt; elements" />
623+
... <meta name="DC.format" scheme="DCTERMS.IMT" content="text/html" />
624+
... <meta name="DC.type" scheme="DCTERMS.DCMIType" content="Text" />
625+
... <meta name="DC.Date.modified" content="2001-07-18" />
626+
... <meta name="DCTERMS.modified" content="2001-07-18" />'''
627+
>>> dublinlde = DublinCoreExtractor()
628+
>>> data = dublinlde.extract(html)
629+
>>> pp.pprint(data)
630+
[ { 'elements': [ { 'URI': 'http://purl.org/dc/elements/1.1/title',
631+
'content': 'Expressing Dublin Core\n'
632+
'in HTML/XHTML meta and link elements',
633+
'lang': 'en',
634+
'name': 'DC.title'},
635+
{ 'URI': 'http://purl.org/dc/elements/1.1/creator',
636+
'content': 'Andy Powell, UKOLN, University of Bath',
637+
'name': 'DC.creator'},
638+
{ 'URI': 'http://purl.org/dc/elements/1.1/identifier',
639+
'content': 'http://dublincore.org/documents/dcq-html/',
640+
'name': 'DC.identifier',
641+
'scheme': 'DCTERMS.URI'},
642+
{ 'URI': 'http://purl.org/dc/elements/1.1/format',
643+
'content': 'text/html',
644+
'name': 'DC.format',
645+
'scheme': 'DCTERMS.IMT'},
646+
{ 'URI': 'http://purl.org/dc/elements/1.1/type',
647+
'content': 'Text',
648+
'name': 'DC.type',
649+
'scheme': 'DCTERMS.DCMIType'}],
650+
'namespaces': { 'DC': 'http://purl.org/dc/elements/1.1/',
651+
'DCTERMS': 'http://purl.org/dc/terms/'},
652+
'terms': [ { 'URI': 'http://purl.org/dc/terms/issued',
653+
'content': '2003-11-01',
654+
'name': 'DCTERMS.issued',
655+
'scheme': 'DCTERMS.W3CDTF'},
656+
{ 'URI': 'http://purl.org/dc/terms/abstract',
657+
'content': 'This document describes how\n'
658+
'qualified Dublin Core metadata can be encoded\n'
659+
'in HTML/XHTML <meta> elements',
660+
'name': 'DCTERMS.abstract'},
661+
{ 'URI': 'http://purl.org/dc/terms/modified',
662+
'content': '2001-07-18',
663+
'name': 'DC.Date.modified'},
664+
{ 'URI': 'http://purl.org/dc/terms/modified',
665+
'content': '2001-07-18',
666+
'name': 'DCTERMS.modified'},
667+
{ 'URI': 'http://purl.org/dc/terms/replaces',
668+
'href': 'http://dublincore.org/documents/2000/08/15/dcq-html/',
669+
'hreflang': 'en',
670+
'rel': 'DCTERMS.replaces'}]}]
671+
672+
587673

588674
Command Line Tool
589675
-----------------
@@ -622,7 +708,7 @@ those, you can pass their individual names collected in a list through 'syntaxes
622708
For example, this command extracts only Microdata and JSON-LD metadata from
623709
"http://example.com"::
624710

625-
extruct "http://example.com" --syntaxes microdata json-ld
711+
extruct "http://example.com" --syntaxes microdata json-ld
626712

627713
NB syntaxes names passed must correspond to these: microdata, json-ld, rdfa, opengraph, microformat
628714

@@ -649,15 +735,3 @@ Use tox_ to run tests with different Python versions::
649735

650736

651737
.. _tox: https://testrun.org/tox/latest/
652-
653-
654-
Versioning
655-
----------
656-
657-
Use bumpversion_ to conveniently change project version::
658-
659-
bumpversion patch # 0.0.0 -> 0.0.1
660-
bumpversion minor # 0.0.1 -> 0.1.0
661-
bumpversion major # 0.1.0 -> 1.0.0
662-
663-
.. _bumpversion: https://pypi.python.org/pypi/bumpversion

extruct/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.9.0
1+
0.10.0

extruct/_extruct.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
from extruct.w3cmicrodata import MicrodataExtractor
77
from extruct.opengraph import OpenGraphExtractor
88
from extruct.microformat import MicroformatExtractor
9-
from extruct.uniform import _umicrodata_microformat, _uopengraph
9+
from extruct.dublincore import DublinCoreExtractor
10+
from extruct.uniform import _umicrodata_microformat, _uopengraph, _udublincore
1011
from extruct.utils import parse_xmldom_html
1112

1213
logger = logging.getLogger(__name__)
13-
SYNTAXES = ['microdata', 'opengraph', 'json-ld', 'microformat', 'rdfa']
14+
SYNTAXES = ['microdata', 'opengraph', 'json-ld', 'microformat', 'rdfa', 'dublincore']
1415

1516

1617
def extract(htmlstring,
@@ -96,6 +97,11 @@ def extract(htmlstring,
9697
('rdfa', RDFaExtractor().extract_items,
9798
tree,
9899
))
100+
if 'dublincore' in syntaxes:
101+
processors.append(
102+
('dublincore', DublinCoreExtractor().extract_items,
103+
tree,
104+
))
99105
output = {}
100106
for syntax, extract, document in processors:
101107
try:
@@ -132,10 +138,20 @@ def extract(htmlstring,
132138
output['opengraph'],
133139
None,
134140
))
141+
if 'dublincore' in syntaxes:
142+
uniform_processors.append(
143+
('dublincore',
144+
_udublincore,
145+
output['dublincore'],
146+
None,
147+
))
148+
135149
for syntax, uniform, raw, schema_context in uniform_processors:
136150
try:
137151
if syntax == 'opengraph':
138152
output[syntax] = uniform(raw, with_og_array=with_og_array)
153+
elif syntax == 'dublincore':
154+
output[syntax] = uniform(raw)
139155
else:
140156
output[syntax] = uniform(raw, schema_context)
141157
except Exception as e:

0 commit comments

Comments
 (0)