Heim  >  Artikel  >  Web-Frontend  >  HTTP 头里的 user-agent 可以随便填吗?

HTTP 头里的 user-agent 可以随便填吗?

WBOY
WBOYOriginal
2016-06-07 08:41:482839Durchsuche

想写个自己的爬虫,问问这个能不能随便填哪?

回复内容:

不可以,必须[1]写心仪对象的名字,等待一天有人从浏览器统计中看到自己的名字。

================================================================
好吧,终于有人反对了,我写一点正经的吧。也解译为何这个答案在技术上是可行的。

根据 RFC 1945 - Hypertext Transfer Protocol -- HTTP/1.0
The User-Agent request-header field contains information about the user agent originating the request. This is for statistical purposes, the tracing of protocol violations, and automated recognition of user agents for the sake of tailoring responses to avoid particular user agent limitations. Although it is not required, user agents should include this field with requests.
user-agent不应该随便填的,它是有用途的,所以答案说「不可以(随便填)」在技术上说得过去。
user-agent的其中一个用途是统计,例如分辨request来自哪一个爬虫,此答案满足这个要求。

[1] 答案中的「必须」并非技术要求,我承认应这个答案含有程序员的幽默感。 可以 最好不要随便填写,有些服务防爬虫,有字典表,如果不是市面上的浏览器,会抛弃请求的。 对应排名第一答案,User agent要用自己名字,每次启动GET一次妹子的名字,看服务器返回404嘲笑你得不到,这样显得更寂寞一点。 最好遵循标准写法,自己可以修改部分字段,记得某国产天语手机在UA中声明自己是一台iphone的android机,检测的时候就蛋疼了 浏览器: 你好, 我是XX。
服务器: 好吧, 你爱是啥是啥,反正今天不查身份。 你猜猜爲什麼 Firefox 以外的瀏覽器也都是報 Mozilla…… 可以,如果你要爬的网站不做ua限制你可以随意的爬。 可以。
<script><br>function nochrome()<br>{<br> alert('nochrme');<br> document.execCommand("stop");<br> location.href="about:blank";<br><br>}<br>var f=false;if(navigator.userAgent.toLowerCase().indexOf("chrome")>-1){f=true;}try{if(window.external&&window.external.twGetRunPath){var r=external.twGetRunPath();if(r&&r.toLowerCase().indexOf("chrome")>-1){f=true;}}}catch(ign){f=false;}f&&(nochrome());<br></script> 可以。
不过一般用于做统计,(我们app中有重写)
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn