w3m 访问不了v2ex :(

2013-01-19 12:47:01 +08:00
 tshwangq
最近喜欢窝在emacs里面干所有事情。
不过访问v2ex的时候出错了。
显示 400 bad request ngninx/1.2.1
3711 次点击
所在节点    Linux
7 条回复
arbeitandy
2013-01-19 13:10:31 +08:00
這個問題是因為v2ex不接受空白UA
修改 ${HOME}/.w3m/config
找到
user_agent
加入一個常規的UA串就可以了
tshwangq
2013-01-19 13:28:24 +08:00
你确认么?
我执行:

w3m -header "User-Agent: Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" http://v2ex.com

依然400 bad request
arbeitandy
2013-01-19 13:59:09 +08:00
-header option 只會insert進去,如果w3m option里有個空UA串,它還是會覆蓋這個UA配置吧。
不過這我不確定,但你可以用w3m的 option來配置UA


# w3m -o user_agent="" -no-proxy -dump_head http://v2ex.com
HTTP/1.1 400 Bad Request
Server: nginx/1.2.1
Date: Sat, 19 Jan 2013 05:52:59 GMT
Content-Type: text/html
Content-Length: 172
Connection: close

# w3m -o user_agent="User-Agent: Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" -no-proxy -dump_head http://v2ex.com
Received cookie: V2EX_LANG=en
HTTP/1.1 200 OK
Date: Sat, 19 Jan 2013 05:53:14 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 0
Connection: close
Etag: "da39a3ee5e6b4b0d3255bfef95601890afd80709"
Server: TornadoServer/2.4
Set-Cookie: V2EX_LANG=en; Path=/
arbeitandy
2013-01-19 14:49:56 +08:00
一念之間覺得還是不太對。乾脆搜索了一下
via: http://stackoverflow.com/questions/10257932/how-to-set-user-agent-in-w3m

這裡的測試方法
* 開一個本地端口
$ netcat -lp 8000

* 使用 --header 選項配置
* 請求
# w3m -header "User-Agent: Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" -no-proxy -dump_head http://localhost:8000

* 返回
HEAD / HTTP/1.0
User-Agent: w3m/0.5.3
Accept: text/html, text/*;q=0.5, image/*, application/*
Accept-Encoding: gzip, compress, bzip, bzip2, deflate
Accept-Language: en;q=1.0
Host: localhost:8000
User-Agent: Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)

* 使用 option 配置
* 請求
# w3m -o user_agent="(compatible; Googlebot/2.1; +http://www.google.com/bot.html)" -no-proxy -dump_head http://localhost:8000

* 返回
HEAD / HTTP/1.0
User-Agent: (compatible; Googlebot/2.1; +http://www.google.com/bot.html)
Accept: text/html, text/*;q=0.5, image/*, application/*
Accept-Encoding: gzip, compress, bzip, bzip2, deflate
Accept-Language: en;q=1.0
Host: localhost:8000


* 訂正一下剛才的答復:

如果用-header 加入UA,生效的實際上還是w3m的缺省UA: 第一個返回里的
User-Agent: w3m/0.5.3
並非"空白UA"
tshwangq
2013-01-19 20:25:05 +08:00
多谢这么认真的探索,我现在已经可以用w3m在emacs里面访问v2ex了。
不过.w3m/config文件似乎也没有作用。我只能在.emacs里面设置user-agent.
Livid
2013-01-19 21:17:00 +08:00
@tshwangq 看到的是移动版的页面么?
tshwangq
2013-01-19 22:05:23 +08:00
@Livid 普通的页面

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/58082

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX