jmcarp / robobrowser

BSD 3-Clause "New" or "Revised" License
3.7k stars 337 forks source link

TypeError: __init__() got an unexpected keyword argument 'headers' #52

Open zajalo opened 8 years ago

zajalo commented 8 years ago
TypeError: __init__() got an unexpected keyword argument 'headers'

This is what I get when I run this within a Flask app like this:

from flask import Flask
app = Flask(__name__)

from robobrowser import RoboBrowser

@app.route("/")
def hello():

    browser = RoboBrowser(headers={'User-Agent': 'a python robot'})
    browser.open('http://google.com/')

    return 'Hello'

if __name__ == "__main__":
    app.run(host="localhost", debug=True)

What could be causing this?

I installed RoboBrowser in a virtualenv using pip 7.1.2, python 2.7.10, Windows 10

zajalo commented 8 years ago

Ok, I didn't even look at the source at first. Turns out, the doc on readthedocs.org is for a different version and the headers param was removed in the current version. It seems now you would have to pass in a requests.Session().

I didn't want to do it that way so I went into robobrowser.browser and added a headers param to __init__, then simply changed this:

        if user_agent is not None:
            self.session.headers['User-Agent'] = user_agent

...into this:

        if headers is not None:
            self.session.headers.update(headers)

PS: Hats off for the nicely organized, clean code

sojala commented 8 years ago

There is user_agent param. If you only need to set the User-Agent header, you could use:

browser = RoboBrowser(user_agent='a python robot')
zajalo commented 8 years ago

@sojala I know, but I wanted to set other header values too.