基于Llamaindex的网页内容爬取实战

目的

本文不关注如何解析网页 html 元素和各种 python 爬虫技术,仅作为一种网页数据的预处理手段进行研究。Llamaindex 也并不是爬虫技术的集大成者,使用它是为了后续的存查一体化。

安装依赖

shell 复制代码
pip install llama-index-readers-web
# pip install llama_index.embeddings.huggingface
# pip install llama_index.llms.ollama

注释部分是补充安装的内容。

测试一下

vim test-web-bs.py,官方示例默认代码:

python 复制代码
from llama_index.core import VectorStoreIndex, download_loader

from llama_index.readers.web import BeautifulSoupWebReader

loader = BeautifulSoupWebReader()
documents = loader.load_data(urls=["https://google.com"])
index = VectorStoreIndex.from_documents(documents)
index.query("What language is on this website?")

上述这个代码是访问 openai 的,Google 也打不开,运行不了:

shell 复制代码
Could not load OpenAI embedding model. If you intended to use OpenAI, please check your OPENAI_API_KEY.
Original error:
No API key found for OpenAI.

而且单独使用 index.query("What language is on this website?") 也报错:AttributeError: 'VectorStoreIndex' object has no attribute 'query',大修,运行:

shell 复制代码
from llama_index.core import VectorStoreIndex, download_loader
from llama_index.core import Settings

from llama_index.readers.web import BeautifulSoupWebReader
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.llms.ollama import Ollama

Settings.embed_model = HuggingFaceEmbedding(
    model_name="/root/RAGAll/models/bge-large-zh-v1.5"  # 替换为你的本地模型路径
)
Settings.llm = Ollama(
    base_url="http://10.11.12.13:11434",
    model="qwen2.5_7b",
    context_window=4096,
    request_timeout=120.0
)
loader = BeautifulSoupWebReader()
documents = loader.load_data(urls=["https://mp.weixin.qq.com/s/xxx-yyy"])
#print(documents)
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine(similarity_top_k=5, streaming=True)
your_query = "本文主要讲了什么?"
#print(query_engine.query(your_query).response)
response = query_engine.query(your_query)
response.print_response_stream()

改善一下

打印上面的 documents 观察到获取到的正文内容无用字符,边角料颇多。使用下面的 Loader,获取到的正文效果好很多。

python 复制代码
from llama_index.readers.web import UnstructuredURLLoader
urls = [
    "https://mp.weixin.qq.com/s/xyz"
]

loader = UnstructuredURLLoader(
    urls=urls, continue_on_failure=False, headers={"User-Agent": "value"}
)

documents = loader.load_data()
print(documents)

报错 AttributeError: 'VectorStoreIndex' object has no attribute 'query'

关于这个报错,查阅了官方文档,VectorStoreIndex 的确是没有 query 这个方法的,所以应该是官方示例 demo 写错了。

python 复制代码
documents = loader.load_data(urls=["https://www.baidu.com"])
index = VectorStoreIndex.from_documents(documents).as_query_engine()
# 然后才可调用query方法
res = index.query("What language is on this website?")
# The language on this website is Chinese
相关推荐
肖永威2 小时前
macOS环境安装/卸载python实践笔记
笔记·python·macos
TechWJ3 小时前
PyPTO编程范式深度解读:让NPU开发像写Python一样简单
开发语言·python·cann·pypto
枷锁—sha3 小时前
【SRC】SQL注入WAF 绕过应对策略(二)
网络·数据库·python·sql·安全·网络安全
abluckyboy3 小时前
Java 实现求 n 的 n^n 次方的最后一位数字
java·python·算法
喵手3 小时前
Python爬虫实战:构建各地统计局数据发布板块的自动化索引爬虫(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集数据csv导出·采集各地统计局数据发布数据·统计局数据采集
天天爱吃肉82184 小时前
跟着创意天才周杰伦学新能源汽车研发测试!3年从工程师到领域专家的成长秘籍!
数据库·python·算法·分类·汽车
m0_715575344 小时前
使用PyTorch构建你的第一个神经网络
jvm·数据库·python
甄心爱学习4 小时前
【leetcode】判断平衡二叉树
python·算法·leetcode
深蓝电商API4 小时前
滑块验证码破解思路与常见绕过方法
爬虫·python
Ulyanov4 小时前
Pymunk物理引擎深度解析:从入门到实战的2D物理模拟全攻略
python·游戏开发·pygame·物理引擎·pymunk