Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(okla): Improve Okla HTML cleanup #1218

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion juriscraper/opinions/united_states/state/okla.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from lxml import html

from juriscraper.lib.html_utils import strip_bad_html_tags_insecure
from juriscraper.OpinionSiteLinear import OpinionSiteLinear


Expand Down Expand Up @@ -45,7 +46,29 @@ def _process_html(self):

@staticmethod
def cleanup_content(content):
tree = html.fromstring(content)
"""Remove non-opinion HTML

:param content: The scraped HTML
:return: Cleaner HTML
"""
tree = strip_bad_html_tags_insecure(content, remove_scripts=True)
for removal_class in ["tmp-citationizer", "footer"]:
for element in tree.xpath(f"//div[@class='{removal_class}']"):
parent = element.getparent()
if parent is not None:
parent.remove(element)

opinions_navigation = tree.xpath("//div[@id='opinons-navigation']")
if opinions_navigation:
opinions_navigation = opinions_navigation[0]
parent = opinions_navigation.getparent()

# Remove all preceding siblings
for sibling in opinions_navigation.itersiblings(preceding=True):
parent.remove(sibling)
opinions_navigation.getparent().remove(opinions_navigation)

# Find the core element with id 'oscn-content'
core_element = tree.xpath("//*[@id='oscn-content']")[0]
return html.tostring(
core_element, pretty_print=True, encoding="unicode"
Expand Down
Loading