【问题标题】:How to get mechanize requests to look like they originate from a real browser如何让机械化请求看起来像是来自真实的浏览器
【发布时间】:2011-01-07 05:23:42
【问题描述】:

好的,这是我在登录帐户时从 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

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

【问题讨论】:

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


    【解决方案1】:

    这取决于您要“愚弄”什么。您可以尝试一些在线服务,通过简单的用户代理嗅探来衡量您的成功:

    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 的情况下捕获发送的标头。

    【讨论】:

    • 有监控脚本网络连接的工具吗?所以我会知道服务器和我的脚本之间传输的数据。
    • JavaScript? Live HTTP Headers 应该这样做。 Python脚本?更难,但像 WireShark 这样的数据包嗅探软件会做到这一点。在这两种情况下,您都会收到很多非脚本噪音,但它仍然可以为您提供很多有用的信息。
    【解决方案2】:

    以下是为 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了解更多信息

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 2019-04-07
        • 2017-09-29
        • 1970-01-01
        • 2017-05-07
        • 2013-12-11
        • 2013-04-12
        • 2013-09-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多