本文介绍了Python Mechanize-如何在单个.open()调用中添加标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图解决某种没有API的服务,因此决定尝试机械化(我通常使用urllib).

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

如何为一个open呼叫添加特定的标头?

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

或者有没有一种方法可以用自己的标头构造一个Request实例,然后让我的mechanize.Browser实例处理它?

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'}))

我的解决方法是临时修改addheaders列表,但是哇,真丑!

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()

推荐答案

这样做:

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()

这篇关于Python Mechanize-如何在单个.open()调用中添加标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 16:46