Skip to content

Determine title from content if <title> is missing #863

Open
@Mr0grog

Description

@Mr0grog

When importing new versions of HTML pages (either from Wayback’s Memento API or from WARCs), we look for the page’s <title> element or use the empty string:

def extract_title(content_bytes, encoding='utf-8'):
"Return content of <title> tag as string. On failure return empty string."
content_str = content_bytes.decode(encoding=encoding, errors='ignore')
# The parser expects a file-like, so we mock one.
content_as_file = io.StringIO(content_str)
try:
title = lxml.html.parse(content_as_file).find(".//title")
except Exception:
return ''
if title is None or title.text is None:
return ''

There are a bunch of pages that turn out to be missing <title> elements, so it would probably be good to fall back to looking for the <h1> or some other title-like information in the page body.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions