用途
lynx-web-search 用于在仅能使用终端的场景下进行互联网研究,作为快速、纯文本的网页搜索与页面获取回退方案。它不依赖浏览器自动化或图形界面,适合需要纯文本输出以便分析或摘要、以及需要将结果确定性地保存为本地文件的工作流。
主要能力
该技能聚焦两个可重复任务:
- 从终端使用搜索引擎进行搜索。
- 抓取指定 URL,并将可读文本转储保存到 /tmp。
支持的搜索引擎及其 URL 模板:
| 引擎 | URL 模板 |
|---|---|
| https://www.google.com/search?q=<query> | |
| Brave | https://search.brave.com/search?q=<query>&source=web |
| Bing | https://www.bing.com/search?q=<query> |
| Yahoo | https://search.yahoo.com/search?p=<query> |
| GitHub | https://github.com/search?q=<query>&type=repositories |
| https://www.reddit.com/search/?q=<query> | |
| Reddit(较少依赖 JS 的回退) | https://old.reddit.com/search?q=<query> |
核心 lynx 标志(来自 man lynx):
-dump:渲染可读文本并退出。-source:转储原始源码并退出。-listonly:仅输出链接列表(适合提取 URL)。-accept_all_cookies:避免 cookie 提示。-useragent=...:当站点阻止默认行为时覆盖 UA。
使用方式
1)搜索任意引擎并将输出保存到 /tmp
engine="brave" # google|brave|bing|yahoo|github|reddit|oldreddit
query="lynx cli usage"
q=$(python - <<'PY' "$query"
import sys, urllib.parse
print(urllib.parse.quote_plus(sys.argv[1]))
PY
)
case "$engine" in
google) url="https://www.google.com/search?q=$q" ;;
brave) url="https://search.brave.com/search?q=$q&source=web" ;;
bing) url="https://www.bing.com/search?q=$q" ;;
yahoo) url="https://search.yahoo.com/search?p=$q" ;;
github) url="https://github.com/search?q=$q&type=repositories" ;;
reddit) url="https://www.reddit.com/search/?q=$q" ;;
oldreddit) url="https://old.reddit.com/search?q=$q" ;;
*) echo "Unknown engine: $engine" >&2; exit 2 ;;
esac
out="/tmp/lynx-search-${engine}-$(date +%Y%m%d-%H%M%S).txt"
lynx -accept_all_cookies -dump "$url" | tee "$out"
printf "\nSaved: %s\n" "$out"
2)抓取给定 URL 并将可读转储保存到 /tmp
url="https://example.com"
out="/tmp/lynx-page-$(date +%Y%m%d-%H%M%S).txt"
lynx -accept_all_cookies -dump "$url" > "$out"
printf "Saved readable dump: %s\n" "$out"
3)可选:将原始 HTML/源码保存到 /tmp
url="https://example.com"
out="/tmp/lynx-source-$(date +%Y%m%d-%H%M%S).html"
lynx -source "$url" > "$out"
printf "Saved source dump: %s\n" "$out"
适用场景
- 需要从 shell 快速进行网页搜索,且没有 GUI 或浏览器自动化。
- 专用网页工具不可用。
- 需要纯文本输出用于分析或摘要。
- 需要在 /tmp 中生成确定性的、可追溯的保存产物。
限制
不适用于以下情况:
- 依赖大量 JavaScript、需要交互或登录流程的页面。
- 视觉测试或 DOM 检查(应使用浏览器自动化工具)。
常见错误:
- 忘记对多词查询进行 URL 编码 → 使用
urllib.parse.quote_plus。 - 假设 Google 在文本模式下始终可用(常被阻止或触发 JS 挑战)。
- 被阻止时只用一个引擎 → 改用 Brave/Bing/Yahoo/GitHub/old Reddit 重试。
- 不保存输出 → 始终写入
/tmp/lynx-*.txt以便追溯。 - 期望 lynx 获取 JS 渲染内容 → 动态页面应使用浏览器自动化。