Пример #1
0
def test_xpath(xpath):
    document = Document("<root><a/><b/></root>")
    transformation = Transformation(Rule(xpath, lib.set_text("x")))
    result = transformation(document).root
    assert not result._data_node._exists
    assert first(result.css_select("a")).full_text == "x"
    assert first(result.css_select("b")).full_text == ""
Пример #2
0
def get_publication(node: TagNode, publicationStmt: TagNode):
    date = first(node.css_select("mods|dateIssued")).full_text
    name = first(node.css_select("mods|publisher")).full_text
    place = first(
        node.xpath('./mods:place/mods:placeTerm[@type="text"]')).full_text

    publicationStmt.append_child(
        tag("publisher", tag("name", name)),
        tag("pubPlace", place),
        tag("date", {"type": "creation"}, date),
    )
Пример #3
0
def get_title(node: TagNode, titleStmt: TagNode, biblFull_titleStmt: TagNode):
    non_sort = first(node.css_select("mods|nonSort"))
    main_title = (non_sort.full_text + " ") if non_sort is not None else ""
    main_title += first(node.css_select("mods|title")).full_text
    sub_title = first(node.css_select("mods|subTitle")).full_text

    for target in (titleStmt, biblFull_titleStmt):
        target.append_child(
            tag("title", {"type": "main"}, main_title),
            tag("title", {"type": "sub"}, sub_title),
        )
Пример #4
0
def test_first():
    assert first([]) is None
    assert first([1]) == 1
    assert first((1, 2)) == 1

    root = Document("<root><a/><a/><b/></root>").root
    assert first(root.child_nodes(is_tag_node, lambda x: x.local_name == "c")) is None
    assert (
        first(root.child_nodes(is_tag_node, lambda x: x.local_name == "b")) is root[2]
    )
    assert (
        first(root.child_nodes(is_tag_node, lambda x: x.local_name == "a")) is root[0]
    )

    with pytest.raises(TypeError):
        first({})
Пример #5
0
 def list_persons(previous_result, html: TagNode):
     first(html.css_select("html|body html|ul")).append_child(
         *(html.new_tag_node("li", children=[f"{x[1]}, {x[0]}"])
           for x in previous_result))
Пример #6
0
 def extract_person(node: TagNode, persons):
     persons.append((
         first(node.css_select("name")).full_text,
         first(node.css_select("family-name")).full_text,
     ))
Пример #7
0
 def extract_person(node: TagNode):
     return node.attributes["username"], first(
         node.css_select("name")).full_text