Skip to content

Commit

Permalink
extraction fix: ValueError in table spans (#685)
Browse files Browse the repository at this point in the history
* extraction fix: ValueError in table spans

* review code
  • Loading branch information
adbar authored Aug 30, 2024
1 parent b3aea4a commit ba43965
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 4 additions & 0 deletions tests/unit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,10 @@ def test_table_processing():
result = extract(htmlstring, no_fallback=True, output_format='txt', config=ZERO_CONFIG, include_tables=True)
assert "a | b | |" in result

htmlstring = '<html><body><article><table><tr><td span="2.1">a</td><td>b</td></tr><tr><td>c</td><td>d</td><td>e</td></tr></table></article></body></html>'
result = extract(htmlstring, no_fallback=True, output_format='txt', config=ZERO_CONFIG, include_tables=True)
assert "a | b | |" in result

# MemoryError: https://github.com/adbar/trafilatura/issues/657
htmlstring = '<html><body><article><table><tr><td colspan="9007199254740991">a</td><td>b</td></tr><tr><td>c</td><td>d</td><td>e</td></tr></table></article></body></html>'
result = extract(htmlstring, no_fallback=True, output_format='txt', config=ZERO_CONFIG, include_tables=True)
Expand Down
8 changes: 7 additions & 1 deletion trafilatura/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@

HI_FORMATTING = {'#b': '**', '#i': '*', '#u': '__', '#t': '`'}

MAX_TABLE_WIDTH = 1000


# https://github.com/lxml/lxml/blob/master/src/lxml/html/__init__.py
def delete_element(element: _Element, keep_tail: bool = True) -> None:
Expand Down Expand Up @@ -318,7 +320,11 @@ def process_element(element: _Element, returnlist: List[str], include_formatting
if element.tag == "row":
cell_count = len(element.xpath(".//cell"))
# restrict columns to a maximum of 1000
max_span = min(int(element.get("colspan") or element.get("span", 1)), 1000)
span_info = element.get("colspan") or element.get("span")
if not span_info or not span_info.isdigit():
max_span = 1
else:
max_span = min(int(span_info), MAX_TABLE_WIDTH)
# row ended so draw extra empty cells to match max_span
if cell_count < max_span:
returnlist.append(f'{"|" * (max_span - cell_count)}\n')
Expand Down

0 comments on commit ba43965

Please sign in to comment.