Repository navigation
fix: read file:// URLs built by Path.as_uri(), including Windows drive paths - #2452
Open
tanishqbaweja wants to merge 1 commit into
Open
tanishqbaweja wants to merge 1 commit into
tanishqbaweja wants to merge 1 commit into
Conversation
…e paths
Both crawler strategies stripped the "file://" prefix (or used
urlparse().path) without decoding the URL. Standard file URLs, as built by
Path.as_uri() or copied from a browser, raised FileNotFoundError:
- the path is percent-encoded, so any path with a space failed on every OS
- on Windows the drive follows a slash (file:///C:/...), which left "/C:/..."
- on Windows AsyncHTTPCrawlerStrategy got an empty path for the documented
f"file://{path}" form, because urlparse puts "C:\..." in the netloc
Add _file_url_to_path(): keep the plain prefix strip when that file exists
(so existing callers are unchanged), otherwise decode the URL with
url2pathname(). Use it in the fast path, the browser path and the HTTP
strategy.
tanishqbaweja
force-pushed
the
fix/file-url-local-paths
branch
from
October 28, 2026 07:47
89ceea9 to
57cbbd1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Standard
file://URLs, such as those built byPath.as_uri()or copied from a browser's address bar, fail withFileNotFoundErrorwhen the path contains a space or any other percent-encoded character. On Windows they fail for every path. I found this on Windows and couldn't find an existing issue for it.AsyncPlaywrightCrawlerStrategytakesurl[7:]without decoding the URL. On Windows,file:///C:/Users/me/page.htmlturns into/C:/Users/me/page.html, which doesn't exist. The path also stays percent-encoded, so on every OS a path with a space fails (my%20docs).AsyncHTTPCrawlerStrategyusesurlparse(url).path, which has the same two problems. On Windows it also breaks the documentedf"file://{path}"form:urlparseputsC:\Users\...in the netloc, so the path is empty (Local file not found:).The fix adds a small
_file_url_to_path()helper. If the file at the plain prefix-stripped path exists, the helper returns that path, so URLs that work today behave as before. Otherwise it decodes the URL withurl2pathname(), which handles standard file URLs, includingfile://localhost/.... The fast path, the browser path and the HTTP strategy all use it now. A missing file still raisesFileNotFoundError.List of files changed and why
crawl4ai/async_crawler_strategy.py: adds_file_url_to_path()next to the other shared helper,_safe_download_filepath(), and uses it at the threefile://sites (Playwright fast path, Playwright browser path,AsyncHTTPCrawlerStrategy.crawl).tests/test_file_url_local_paths.py: new regression tests. Both strategies read a file throughPath.as_uri()and throughf"file://{path}", from a folder with a space in its name. A missing file still raisesFileNotFoundError. No browser or network is needed.Not changed here:
PDFContentScrapingStrategy._get_pdf_pathstill strips the prefix the same way. It could use the same helper in a follow-up.How Has This Been Tested?
I ran everything natively on Windows 11 with Python 3.12 and Playwright Chromium.
pytest tests/test_file_url_local_paths.pydevelop: 3 failed, 5 passed. Playwrightas_urifails withLocal file not found: /C:/Users/.../my%20docs/page.html, and so does HTTPas_uri. HTTPf"file://{path}"fails withLocal file not found:(an empty path).End to end with
AsyncWebCrawler.arun()on a page in a folder namedmy docs, for each URL form withprocess_in_browserset to False and to True:process_in_browserdeveloppage.as_uri()FileNotFoundErrorpage.as_uri()FileNotFoundErrorf"file://{page}"f"file://{page}"pytest tests/test_raw_html_edge_cases.py -k file_url:test_file_url_fast_pathandtest_file_url_with_js_codefail the same way ondevelopand with this change. The file is read, but the anti-bot check rejects the small test pages ("Near-empty content (48 bytes)" and "(89 bytes) with HTTP 200"), so the failures are unrelated to this change.black --check tests/test_file_url_local_paths.pypasses.async_crawler_strategy.pyisn't black-formatted ondevelop, so I didn't reformat it; black leaves the changed lines as they are.Checklist:
f"file://{path}"form keeps working)file_urltests intest_raw_html_edge_cases.pyfail ondeveloptoo, as described above)