爬虫(关于大模型+爬虫技术)
两个开源爬虫项目地址
crawl4ai
安装:
pip install crawl4ai
pip install playweight
playweight install
示例:
import asyncio
from crawl4ai import AsyncWebCrawler
import json
# 定义如何从页面中提取数据。
from crawl4ai.extraction_strategy import JsonCssExtractionStrategy
async def main():
async with AsyncWebCrawler(verbose=True) as crawler:
result = await crawler.arun(url="https://lpt.liepin.com/recommend#preview")
print(f"基本爬取结果: {result.markdown[:500]}") # 打印前500个字符
asyncio.run(main())
ScrapeGraphAi
安装
pip install ScrapeGraphAi
pip install playweight
playweight install
示例:
import json
from langchain import requests
from scrapegraphai.graphs import SmartScraperGraph
from langchain_community.chat_models.moonshot import MoonshotChat
import os
from langchain_community.chat_models import QianfanChatEndpoint
from langchain_core.language_models.chat_models import HumanMessage
#
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Connection': 'close',
}
response=requests.get("xxxx",headers=headers)
os.environ["QIANFAN_AK"] = "xxxx"
os.environ["QIANFAN_SK"] = "xxxx"
chat = QianfanChatEndpoint(streaming=True)
graph_config = {
"llm": {
"model_instance": chat,
"model_tokens": 5000
},
}
# 创建实例
smart_scraper_graph = SmartScraperGraph(
prompt="找到这个网站是做什么的,xxxx。",
source="https://lpt.liepin.com/recommend#preview",
config=graph_config
)
# 运行爬虫
result = smart_scraper_graph.run()
# 输出结果
print(json.dumps(result, indent=4))