基于DDGS实现图片搜索,文本搜索,新闻搜索

1、如何安装ddgs模块

在pycharm当中我们可以使用pip的安装方式直接安装ddgs库这里,我们提供安装方法,安装指定版本号方法,安装虚拟环境。

①安装方法,直接使用pip安装。

bash 复制代码
pip install ddgs

②安装版本号,可安装特定的版本号。

bash 复制代码
pip install ddgs==版本号

③虚拟环境安装方法

bash 复制代码
python -m venv myenv
# 激活虚拟环境
# Windows:
myenv\Scripts\activate
# macOS/Linux:
source myenv/bin/activate
# 安装 ddgs
pip install ddgs

2、ddgs的基本使用方法

现在我来展示ddgs的基本使用方法,可以进行全网的文本搜索,图片搜索,视频搜索功能。

2.1ddgs的文本搜索功能

python 复制代码
from ddgs import DDGS
import time
with DDGS() as ddgs:
 results = ddgs.text('Python 编程', max_results=5)
 for result in results:
 print(f"标题: {result['title']}")
 print(f"链接: {result['href']}")
 print(f"摘要: {result['body']}")
 print("---")
 time.sleep(1) # 添加1秒延迟

文本搜索效果:

我设定了地域为中国,所以搜索的全部都是中国的网站。

2.2图片搜索功能

python 复制代码
from ddgs import DDGS
import time
with DDGS() as ddgs:
 results = ddgs.images('冰剑龙', max_results=3)
 for result in results:
 print(f"图⽚标题: {result['title']}")
 print(f"图⽚URL: {result['image']}")
 print(f"来源: {result['source']}")
 print("---")
 time.sleep(1) # 添加1秒延迟

图片搜索效果展示:

我设定了搜索的图片名称为冰剑龙,且设定了region为中国这是他的效果。

2.3新闻搜索功能

python 复制代码
from ddgs import DDGS
import time
with DDGS() as ddgs:
 results = ddgs.news('⼈⼯智能', max_results=3)
 for result in results:
 print(f"新闻标题: {result['title']}")
 print(f"链接: {result['url']}")
 print(f"⽇期: {result['date']}")
 print(f"摘要: {result['body']}")
 print("---")
 time.sleep(1) # 添加1秒延迟

新闻搜索效果展示:

3、总结

DDGS可以直接通过设置地域的方式来查询我们想要得到的内容,功能十分强大,相较于我们自己去写一个爬虫,省时又省力,规避掉了很多麻烦的过程。

相关推荐
Ray Liang10 分钟前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
中杯可乐多加冰24 分钟前
OpenClaw到底能做什么?有什么用?先装这几个实用的Skills
人工智能
AI攻城狮25 分钟前
如何给 AI Agent 做"断舍离":OpenClaw Session 自动清理实践
python
千寻girling28 分钟前
一份不可多得的 《 Python 》语言教程
人工智能·后端·python
aircrushin3 小时前
从春晚看分布式实时协同算法与灵巧手工程实现
人工智能·机器人
恋猫de小郭3 小时前
Apple 的 ANE 被挖掘,AI 硬件公开,宣传的 38 TOPS 居然是"数字游戏"?
前端·人工智能·ios
银河系搭车客指南3 小时前
AI Agent 的失忆症:我是怎么给它装上"第二个大脑"的
人工智能
张拭心3 小时前
春节后,有些公司明确要求 AI 经验了
android·前端·人工智能
我的username3 小时前
极致简单的openclaw安装教程
人工智能