6.6. Clause

6.6.1. OR

class easydata.parsers.clause.OR(*args, strict_none: bool = False)[source]

Bases: easydata.parsers.base.Base

Example:

Lets import our easydata module first.

>>> import easydata as ed

Lets write our OR parser.

test_html = '''
    <p class="brand">EasyData</p>
'''

or_parser = ed.OR(
    ed.Text(ed.pq('.brand-wrong-selector::text')),
    ed.Text(ed.pq('.brand::text')),
)

Now lets parse test_html data and print our result.

print(or_parser.parse(test_html))
'EasyData'

First our parsers.Text(pq('.brand-wrong-selector::text')), output was None, while next Text parser in line has produced output, since it’s selector was able to extract data from HTML.

Please note that even if query selector found a match and it’s content was still None, then data from the next parser in line would be tried to be parsed.

Another example:

test_html = '''
    <p class="brand">EasyData</p>
    <p id="name">Easybook Pro 13</p>
'''

or_parser = ed.Or(
    ed.Text(ed.pq('#name::text')),
    ed.Text(ed.pq('.brand::text'))
)

Now lets parse test_html data and print our result.

print(or_parser.parse(test_html))
'Easybook Pro 13'

In this case, data parsed in a first parser was returned, since it’s css selector was able to find this time data and because of that, Text parser returned a value. All other parsers further down the line are ignored when first match is found.

Parameters

strict_none

6.6.2. WITH

class easydata.parsers.clause.WITH(*args, strict_none: bool = False)[source]

Bases: easydata.parsers.clause.OR

Example:

Lets import our easydata module

>>> import easydata as ed

Lets write our WITH parser.

test_html = '''
    <div id="description">
        <ul class="features">
            <li>Material: aluminium <span>MATERIAL</span></li>
            <li>style: <strong>elegant</strong> is this</li>
            <li>Date added: Fri, 12 Dec 2018 10:55</li>
        </ul>
    </div>
'''

with_parser = ed.WITH(
    ed.Sentences(
        ed.pq('#description .features::text'),
        allow=['date added'],
    ),
    ed.DateTimeSearch(),
)

Now lets parse test_html data and print our result.

print(with_parser.parse(test_html))
'12/12/2018 10:55:00'

6.6.3. SWITCH

class easydata.parsers.clause.SWITCH(parser: Union[easydata.parsers.base.Base, easydata.parsers.base.BaseData, Callable], *cases, default: Any = None)[source]

Bases: easydata.parsers.base.Base

examples coming soon …

Parameters

default

6.6.4. IF

class easydata.parsers.clause.IF(if_parser: Union[easydata.parsers.base.Base, easydata.parsers.base.BaseData, Callable], then_parser: Any, else_parser: Optional[Any] = None, condition: Optional[Callable] = None)[source]

Bases: easydata.parsers.base.Base

examples coming soon …

Parameters

if_parser
then_parser
else_parser
condition