Lxml html clean fix
2025-06-016 turns9,273 charsgpt-4o
Summary
User was debugging Python script errors related to lxml HTML cleaning in their scraping pipeline.
Messages
python3 pipeline.py
Traceback (most recent call last):
File "/Users/danielkliewer/obj01/pipeline.py", line 4, in <module>
from modules.scraping import fetch_articles
File "/Users/danielkliewer/obj01/modules/scraping.py", line 3, in <module>
from newspaper import Article
File "/Users/danielkliewer/obj01/venv/lib/python3.13/site-packages/newspaper/__i
nit__.py", line 10, in <module> from .api import (build, build_article, fulltext, hot, languages,
popular_urls, Configuration as Config)
File "/Users/danielkliewer/obj01/venv/lib/python3.13/site-packages/newspaper/api
.py", line 14, in <module> from .article import Article
File "/Users/danielkliewer/obj01/venv/lib/python3.13/site-packages/newspaper/art
icle.py", line 15, in <module> from . import network
File "/Users/danielkliewer/obj01/venv/lib/python3.13/site-packages/newspaper/net
work.py", line 14, in <module> from .configuration import Configuration
File "/Users/danielkliewer/obj01/venv/lib/python3.13/site-packages/newspaper/con
figuration.py", line 15, in <module> from .parsers import Parser
File "/Users/danielkliewer/obj01/venv/lib/python3.13/site-packages/newspaper/par
sers.py", line 12, in <module> import lxml.html.clean
File "/Users/danielkliewer/obj01/venv/lib/python3.13/site-packages/lxml/html/cle
an.py", line 18, in <module> raise ImportError(
...<2 lines>...
) from None
ImportError: lxml.html.clean module is now a separate project lxml_html_clean.
Install lxml[html_clean] or lxml_html_clean directly.
The error you're seeing is due to a **breaking change in the `lxml` library**. The `lxml.html.clean` module was **split into a separate package** called [`lxml_html_clean`](https://pypi.org/project/lxml-html-clean/) starting in recent versions.
### 🔧 Fix
You have two options:
---
### ✅ Option 1: Install the extra dependency with `lxml`
Run this:
```bash
pip install "lxml[html_clean]"
```
This installs the `lxml_html_clean` package automatically as a dependency.
---
### ✅ Option 2: Install `lxml_html_clean` directly
If that fails, you can explicitly install the new package:
```bash
pip install lxml-html-clean
```
---
### ✅ Optional: Pin compatible versions (for long-term stability)
If you're deploying or working in a shared environment, consider pinning versions:
```txt
lxml==4.9.3 # or whatever works with your code
lxml-html-clean==0.1.1
```
Then install with:
```bash
pip install -r requirements.txt
```
---
Let me know if you want help downgrading or patching the `newspaper3k` code to not require cleaning, though it’s easiest to just install the missing dependency.
gpt-4o
python3 pipeline.py
Traceback (most recent call last):
File "/Users/danielkliewer/obj01/pipeline.py", line 5, in <module>
from modules.translation import translate_article
File "/Users/danielkliewer/obj01/modules/translation.py", line 4, in <module>
package.install_from_path('https://argosopentech.nyc3.digitaloceanspaces.com/a
rgos-translate/models/translate-fr_en-1_0.argosmodel') ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/danielkliewer/obj01/venv/lib/python3.13/site-packages/argostranslat
e/package.py", line 249, in install_from_path raise Exception("Not a valid Argos Model (must be a zip archive)")
Exception: Not a valid Argos Model (must be a zip archive)
The error:
```
Exception: Not a valid Argos Model (must be a zip archive)
```
means that `argos-translate` is trying to install a model from a URL, but the file it's downloading is **not a valid `.argosmodel` ZIP archive**, even though it has the `.argosmodel` extension.
### ✅ Fixes
Here’s how to resolve this:
---
### 🔍 Step 1: Check if the model file URL is valid
Test the URL manually:
1. Open this in a browser:
```
https://argosopentech.nyc3.digitaloceanspaces.com/argos-translate/models/translate-fr_en-1_0.argosmodel
```
2. If it **404s or downloads a broken file**, that’s the issue.
---
### ✅ Step 2: Download manually, then install locally
Instead of installing from a remote URL, try this:
```bash
# Download manually
curl -O https://argosopentech.nyc3.digitaloceanspaces.com/argos-translate/models/translate-fr_en-1_0.argosmodel
# Then install locally
python3
>>> from argostranslate import package
>>> package.install_from_path('translate-fr_en-1_0.argosmodel')
```
Make sure the file is downloaded completely and is **not HTML or a redirect** (check with `file translate-fr_en_1_0.argosmodel`).
---
### 🛠 Alternative: Use `argos-translate-cli` to install models
You can list and install models via the CLI:
```bash
pip install argos-translate
# List available models
argos-translate-cli --list-models
# Install the French to English model
argos-translate-cli --install-model translate fr en
```
---
### ✅ Bonus: Keep a backup of the working `.argosmodel` file
If the URL goes down again in the future (which happens), it’s smart to cache the `.argosmodel` files locally in your project repo or object storage.
---
Let me know if you'd like a script that verifies and installs models automatically, or checks for corrupt downloads.
gpt-4o
python3 pipeline.py
🔧 Installing translation models...
Traceback (most recent call last):
File "/Users/danielkliewer/obj01/pipeline.py", line 5, in <module>
from modules.translation import translate_article
File "/Users/danielkliewer/obj01/modules/translation.py", line 48, in <module>
initialize_translator()
~~~~~~~~~~~~~~~~~~~~~^^
File "/Users/danielkliewer/obj01/modules/translation.py", line 43, in initialize
_translator package.install_from_path(str(model_file))
~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
File "/Users/danielkliewer/obj01/venv/lib/python3.13/site-packages/argostranslat
e/package.py", line 249, in install_from_path raise Exception("Not a valid Argos Model (must be a zip archive)")
Exception: Not a valid Argos Model (must be a zip archive)
Thanks for the update — you're still getting the `Not a valid Argos Model (must be a zip archive)` error, but now it's happening even though you're installing from a **local file (`model_file`)**.
This means the file exists, but it’s **not a valid `.argosmodel` zip archive**, possibly due to one of the following:
---
### 🚩 Common Causes
1. **Partial or corrupted download**
Your code likely tried to download the `.argosmodel` file, but saved an HTML error page instead of the model.
2. **Wrong content-type or redirect URL**
Some hosting services (like DigitalOcean Spaces or GitHub) serve a redirect or HTML page if headers aren’t passed properly during download.
---
### ✅ Step-by-Step Fix
#### 1. Check the downloaded file
Go to the directory where the `.argosmodel` file was downloaded and run:
```bash
file translate-fr_en-1_0.argosmodel
```
If it says something like `HTML document` or `ASCII text`, it's not valid — it's probably an error page.
---
#### 2. Redownload the model manually
Run:
```bash
curl -L -o translate-fr_en_1_0.argosmodel \
https://argosopentech.nyc3.digitaloceanspaces.com/argos-translate/models/translate-fr_en-1_0.argosmodel
```
Then verify:
```bash
unzip -l translate-fr_en_1_0.argosmodel
```
It should show files inside the zip archive (like `metadata.argosmodel`, etc.). If it doesn’t unzip, it's corrupt.
---
#### 3. Manually install the model
In Python:
```python
from argostranslate import package
package.install_from_path('translate-fr_en_1_0.argosmodel')
```
---
### 🧰 Optional: Safer Auto-Download Script
Here’s a snippet you can drop into `translation.py` to verify the file before installing:
```python
import requests
from pathlib import Path
from argostranslate import package
def download_model_safe(url: str, dest: Path):
if not dest.exists() or dest.stat().st_size < 1000:
print(f"📥 Downloading model from {url}")
r = requests.get(url, allow_redirects=True)
if r.status_code == 200 and r.headers.get('Content-Type') == 'application/zip':
dest.write_bytes(r.content)
else:
raise Exception(f"❌ Failed to download model: {r.status_code}, content-type: {r.headers.get('Content-Type')}")
else:
print("✅ Model already exists and looks valid.")
# Usage
model_url = "https://argosopentech.nyc3.digitaloceanspaces.com/argos-translate/models/translate-fr_en-1_0.argosmodel"
model_path = Path("translate-fr_en_1_0.argosmodel")
download_model_safe(model_url, model_path)
package.install_from_path(str(model_path))
```
---
Let me know if you want help writing a fallback to install models interactively via CLI if the download fails.
gpt-4o