使用LangChain的UnstructuredURLLoader加载网页遇依赖及文件类型错误求助
解决LangChain UnstructuredURLLoader加载URL的报错问题
1. 关于python-magic-bin安装失败的说明
- python-magic-bin是Windows平台专用的包,macOS/Linux系统不需要安装它,直接忽略这个错误即可。
2. 解决libmagic不可用的问题
你通过pip安装的libmagic是占位包,并非真正的系统级libmagic库。在macOS上需要通过Homebrew安装原生libmagic:
brew install libmagic
安装完成后,重新运行代码即可让python-magic正确调用系统库。
3. 修复文件类型检测失败(FileType.UNK)的问题
如果上述步骤后仍报错,可通过以下方式调整:
方式一:强制指定文件类型为HTML
修改UnstructuredURLLoader初始化代码,添加content_type参数明确内容类型:
from langchain.document_loaders import UnstructuredURLLoader loader = UnstructuredURLLoader( urls=["https://www.moneycontrol.com/news/business/tata-motors-mahindra-gain-certificates-for-production-linked-payouts-11281691.html"], content_type="text/html" ) data = loader.load()
方式二:更新unstructured库
旧版本可能存在文件类型检测bug,更新到最新版本:
pip install --upgrade unstructured
方式三:使用宽松处理策略
添加strategy="fast"参数跳过严格的文件类型校验:
loader = UnstructuredURLLoader( urls=["https://www.moneycontrol.com/news/business/tata-motors-mahindra-gain-certificates-for-production-linked-payouts-11281691.html"], strategy="fast" )
内容的提问来源于stack exchange,提问作者Debrup Paul
相关产品推荐
相关产品推荐

