如何让机械化请求看起来像是来自真正的浏览器

时间:2011-01-07 05:23:42

标签: python http-headers http-post mechanize mechanize-python

好的,这是我在登录帐户时从Live HTTP Header获得的标题(只是一个示例)信息:

http://example.com/login.html

POST /login.html HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 GTB7.1 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://example.com
Cookie: blahblahblah; blah = blahblah
Content-Type: application/x-www-form-urlencoded
Content-Length: 39
username=shane&password=123456&do=login

HTTP/1.1 200 OK
Date: Sat, 18 Dec 2010 15:41:02 GMT
Server: Apache/2.2.3 (CentOS)
X-Powered-By: PHP/5.2.14
Set-Cookie: blah = blahblah_blah; expires=Sun, 18-Dec-2011 15:41:02 GMT; path=/; domain=.example.com; HttpOnly
Set-Cookie: blah = blahblah; expires=Sun, 18-Dec-2011 15:41:02 GMT; path=/; domain=.example.com; HttpOnly
Set-Cookie: blah = blahblah; expires=Sun, 18-Dec-2011 15:41:02 GMT; path=/; domain=.example.com; HttpOnly
Cache-Control: private, no-cache="set-cookie"
Expires: 0
Pragma: no-cache
Content-Encoding: gzip
Vary: Accept-Encoding
Content-Length: 4135
Keep-Alive: timeout=10, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8

通常我会像这样编码:

import mechanize
import urllib2

MechBrowser = mechanize.Browser()
LoginUrl = "http://example.com/login.html"
LoginData = "username=shane&password=123456&do=login"
LoginHeader = {"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 GTB7.1 (.NET CLR 3.5.30729)", "Referer": "http://example.com"}

LoginRequest = urllib2.Request(LoginUrl, LoginData, LoginHeader)
LoginResponse = MechBrowser.open(LoginRequest)

以上代码工作正常。我的问题是,我是否还需要在LoginHeader中添加以下这些行(以及之前的标题信息中的更多内容),以使它看起来像firefox的冲浪,而不是机械化?

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7

需要欺骗哪些部分/多少标题信息以使其看起来“真实”?

3 个答案:

答案 0 :(得分:6)

这取决于你想要'傻瓜'。您可以尝试一些简单的用户代理嗅探来评估您的成功的在线服务:

http://browserspy.dk/browser.php

http://www.browserscope.org(寻找'我们认为你正在使用......')

http://www.browserscope.org/ua

http://panopticlick.eff.org/ - >将帮助您选择一些“太常见的跟踪”选项

http://networking.ringofsaturn.com/Tools/browser.php

我相信一个坚定的程序员可以检测到你的游戏,但许多日志解析器和工具一旦你回应你的真实浏览器发送的内容就不会。

你应该考虑的一件事是缺少JS可能会引发危险信号,因此捕获已发送的标头也会被禁用JS。

答案 1 :(得分:5)

以下是为mechanize.Browser发出的所有请求设置用户代理的方法

br = mechanize.Browser()
br.addheaders = [('User-agent', 'your user agent string here')]

机械化也可以填写表格

br.open('http://yoursite.com/login')
br.select_form(nr=1) # select second form in page (0 indexed)
br['username'] = 'yourUserName' # inserts into form field with name 'username'
br['password'] = 'yourPassword'
response = br.submit()
if 'Welcome yourUserName' in response.get_data():
    # login was successful
else:
    # something went wrong
    print response.get_data()

有关详细信息,请参阅mechanize examples

答案 2 :(得分:0)

如果你是关于保持机器人/脚本/非真实浏览器的偏执,你会寻找像HTTP请求的顺序,让一个资源使用JavaScript添加。如果在JavaScript之前没有请求或请求该资源 - 那么你就知道它是一个“虚假”的浏览器。 您还可以查看每个连接的请求数(保持活动状态),或者只是验证第一页的所有CSS文件(假设它们位于HTML顶部)都已加载。

YMMV但是模拟足以使某些“虚假”浏览器作为“真实”浏览器传递(由人类使用)会变得非常麻烦。