如何配置w3m浏览器预格式化页面,仅显示目标链接内容?
<a> Links in w3m for Specific Sites 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
sedcommand extracts the link text (\2) and URL (\1) from every<a>tag, formatting them neatly. - Use
-T text/plainto 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:
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 fiMake the script executable:
chmod +x ~/w3m-link-filter.shConfigure 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 thematchcommands to capture the full link text. - Test the script first with
curl your-site-url | ~/w3m-link-filter.shto make sure it’s extracting links correctly before setting it up in w3m.
内容的提问来源于stack exchange,提问作者uwe

