Cloning mechanize.Browser yields to an error

假如想象 提交于 2019-12-08 08:38:52

问题


I am currently trying to send multiple requests with the mechanize.Browser object at once and my approach was to start some threads. Each thread should clone the mechanize.Browser object and send the requests. But copying the object causes a TypeError:

    TypeError: object.__new__(cStringIO.StringO) is not safe, use cStringIO.StringO.__new__()

But I can't figure out how to get it running. After opening some pages I spawn the threads and do:

    newbr = copy.deepcopy(br)

which causes the error.

Any ideas?

Best regards, Chris


回答1:


I faced a similar problem, as addressed on this SO post.

One solution is to follow the link, then use the br.back() function to get back to the initial state later.

If this doesn't work (it didn't for me), if you're not against using other libraries, you can use the very good requests library to follow the link instead. In my case, I wanted to fill in a form and submit it with either post or get, which I was able to do using the following code (it should only need minor modification to follow links instead, using a for links in br.links() loop, for example)

import mechanize
import requests

def testErrorCodes(br,theCodes):
    for x in theCodes:

        br.select_form(nr=0)

        theAction = br.action
        payload = {'code': x}

        response = requests.post(theAction, data=payload)
        print response.status_code

br=mechanize.Browser()
br.set_handle_robots(False)
response = br.open("http://savanttools.com/test-http-status-codes")

testErrorCodes(br,[401,402,403,404,500,503,504]) # Prints the error codes 

testErrorCodes(br,[404]) # The browser is still alive and well to be used again!


来源:https://stackoverflow.com/questions/28835859/cloning-mechanize-browser-yields-to-an-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!