You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置w3m浏览器预格式化页面,仅显示目标链接内容?

Absolutely, you can tailor w3m to display only the <a href=*> links from your target site and hide all extra clutter! Here are two straightforward, practical methods to make this happen:

Method 1: Quick One-Liner with Pipe and Text Processing

If you want an on-the-fly solution without permanent config changes, fetch the page with curl, filter out just the links, and feed the cleaned content to w3m:

curl "https://www.startpage.com/do/dsearch?query=test" | sed -n 's/.*<a href="\([^"]*\)".*>\([^<]*\)<\/a>.*/\2 → \1/p' | w3m -T text/plain
  • The sed command extracts the link text (\2) and URL (\1) from every <a> tag, formatting them neatly.
  • Use -T text/plain to tell w3m to render the filtered output as plain text (no extra HTML parsing needed).

For more robust handling (like sites using single quotes for href or messy HTML), swap sed with awk:

curl "https://www.startpage.com/do/dsearch?query=test" | awk 'BEGIN{IGNORECASE=1} /<a / {
  match($0, /href=["'"'"']?([^"'"'"' >]+)["'"'"']?/, href)
  match($0, />([^<]+)<\/a>/, text)
  if (href[1] && text[1]) { print text[1] " → " href[1] }
}' | w3m -T text/plain

Method 2: Permanent Site-Specific Filter in w3m

For a seamless, permanent setup that automatically filters your target site every time you visit it, use w3m's external HTML filter feature:

  1. Create a filter script (e.g., ~/w3m-link-filter.sh) with the following content:

    #!/bin/bash
    # Check if we're visiting the target site (adjust the URL pattern as needed)
    if [[ "$W3M_URL" == "https://www.startpage.com/"* ]]; then
      # Extract only <a> tags and format them as clean links
      awk 'BEGIN{IGNORECASE=1} /<a / {
        match($0, /href=["'"'"']?([^"'"'"' >]+)["'"'"']?/, href)
        match($0, />([^<]+)<\/a>/, text)
        if (href[1] && text[1]) {
          print "<p><a href=\"" href[1] "\">" text[1] "</a></p>"
        }
      }'
    else
      # Pass through all other sites unchanged
      cat
    fi
    
  2. Make the script executable:

    chmod +x ~/w3m-link-filter.sh
    
  3. Configure w3m to use the filter:
    Open your w3m config file (~/.w3m/config) and add this line:

    ext_filter_html /home/your-username/w3m-link-filter.sh
    

    (Replace /home/your-username/ with your actual home directory path)

Now, whenever you load your target site in w3m, it’ll automatically strip out everything except the <a> links. All other sites will load normally as before.

Pro Tips

  • Adjust the regex patterns in the script to match the exact HTML structure of your target site—some sites might have nested elements inside <a> tags, so you may need to tweak the match commands to capture the full link text.
  • Test the script first with curl your-site-url | ~/w3m-link-filter.sh to make sure it’s extracting links correctly before setting it up in w3m.

内容的提问来源于stack exchange,提问作者uwe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:38:12