Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Mechanize - how to add a header on a single .open() call?

I'm trying to get around a certain service not having an API and decided to try Mechanize (I normally use urllib).

How do I add a specific header for one open call?

Or is there a way to construct a Request instance with its own headers, then have my mechanize.Browser instance handle it?

browser = mechanize.Browser()
headers = [
    ('Accept', 'text/javascript, text/html, application/xml, text/xml, */*'),
    ('Content-type', 'application/x-www-form-urlencoded; charset=UTF-8'),
    ('User-Agent', 'Foobar'),
]

browser.addheaders = headers
# log in, do stuff, etc.

# here, for this one browser request, I need to add an AJAX header
browser.open('/a_url_to_ajax_post/', urllib.urlencode({'foo': 'bar'}))

My workaround is to temporarily modify the addheaders list, but wow that is ugly!

browser.addheaders.append(AJAX_HEADER)
browser.open('/admin/discounts', urllib.urlencode(pulled_params))
browser.addheaders.pop()
like image 258
Yuji 'Tomita' Tomita Avatar asked Jan 16 '23 16:01

Yuji 'Tomita' Tomita


1 Answers

Do it like this:

import mechanize
import urllib2

browser = mechanize.Browser()

# setup your header, add anything you want
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1', 'Referer': 'http://whateveritis.com'}
url = "http://google.com"

# wrap the request. You can replace None with the needed data if it's a POST request
request = urllib2.Request(url, None, header)

# here you go
response = browser.open(request)

print response.geturl()
print response.read()
response.close()
like image 164
Shane Avatar answered Jan 18 '23 22:01

Shane