本文整理汇总了Python中mechanize.install_opener函数的典型用法代码示例。如果您正苦于以下问题:Python install_opener函数的具体用法?Python install_opener怎么用?Python install_opener使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了install_opener函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: retrieve_product_data
def retrieve_product_data(self, product_link):
cookies = mechanize.CookieJar()
opener = mechanize.build_opener(mechanize.HTTPCookieProcessor(cookies))
opener.addheaders = [('User-agent', 'Mozilla/5.0 (MyProgram/0.1)'),
('From', '[email protected]')]
mechanize.install_opener(opener)
browser = mechanize.Browser()
product_data = browser.open(product_link).get_data()
soup = BeautifulSoup(product_data)
product_name = soup.find('title').string.encode('ascii', 'ignore')
product_prices = soup.find('div', 'price').contents
try:
cash_price = int(clean_price_string(product_prices[4]))
product_data = ProductData()
product_data.custom_name = product_name
product_data.price = cash_price
product_data.url = product_link
product_data.comparison_field = product_link
return product_data
except IndexError:
return None
示例2: _retrieve_product
def _retrieve_product(cls, url):
cookies = mechanize.CookieJar()
opener = mechanize.build_opener(mechanize.HTTPCookieProcessor(cookies))
opener.addheaders = [('User-agent', 'Mozilla/5.0 (MyProgram/0.1)'),
('From', '[email protected]')]
mechanize.install_opener(opener)
browser = mechanize.Browser()
product_data = browser.open(url).get_data()
soup = BeautifulSoup(product_data)
product_name = soup.find('h1').string.encode('ascii', 'ignore')
product_price = soup.find('span', {'id': 'product_price'})
product_price = Decimal(clean_price_string(product_price.string))
payment_methods = ['cash', 'deposit', 'wire_transfer']
additional_data = soup.find('td', 'descr').findAll('h3')
if not additional_data:
payment_methods.extend(['debit_card', 'credit_card'])
elif additional_data[0].string and 'Contado' not in \
additional_data[0].string:
payment_methods.extend(['debit_card', 'credit_card'])
prices = {}
for p in payment_methods:
prices[p] = product_price
return [product_name, prices]
示例3: __init__
def __init__(self, username="RP\\12345", password="abcdef"):
self.username = "RP\\"+username
self.password = password
self.password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
ntlm_auth = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(self.password_manager)
opener = mechanize.build_opener(ntlm_auth)
mechanize.install_opener(opener)
示例4: customizeUserAgent
def customizeUserAgent():
import mechanize
cookies = mechanize.CookieJar()
opener = mechanize.build_opener(mechanize.HTTPCookieProcessor(cookies))
# Pretend to be Chrome to avoid getting the mobile site.
opener.addheaders = [("User-agent", "Chrome/16.0.912.63")]
mechanize.install_opener(opener)
示例5: init
def init(self):
br = mechanize.Browser()
br.set_handle_robots(False)
self.cj = mechanize.LWPCookieJar()
br.set_cookiejar(self.cj)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(),
max_time=1)
br.open("https://www.tumblr.com/login")
br.select_form(nr=0)
br['user[email]'] = ""
br['user[password]'] = ""
url, data, hdrs = br.form.click_request_data()
br.open("https://www.tumblr.com/login", data)
self.nf = 0
opener = mechanize.build_opener(
mechanize.HTTPCookieProcessor(self.cj))
mechanize.install_opener(opener)
self._fetch()
示例6: retrieve_product_links
def retrieve_product_links(self):
cookies = mechanize.CookieJar()
opener = mechanize.build_opener(mechanize.HTTPCookieProcessor(cookies))
opener.addheaders = [('User-agent', 'Mozilla/5.0 (MyProgram/0.1)'),
('From', '[email protected]')]
mechanize.install_opener(opener)
url_base = 'http://www.globalmac.cl/'
browser = mechanize.Browser()
url_extensions = [
['Distribuidor-Apple-Chile/MacBook-Air', 'Notebook'],
['Distribuidor-Apple-Chile/MacBook-Pro', 'Notebook'],
['Hardware-Mac-PC/Discos-Duros-Notebook-SATA-2.5', 'StorageDrive'],
['Hardware-Mac-PC/Discos-Duros-SATA-3.5', 'StorageDrive'],
['Hardware-Mac-PC/Discos-Duros-SSD-SATA-2.5', 'StorageDrive'],
]
product_links = []
for url_extension, ptype in url_extensions:
url = url_base + url_extension
base_data = browser.open(url).get_data()
soup = BeautifulSoup(base_data)
for item in soup.findAll('div', 'name'):
product_links.append([item.find('a')['href'], ptype])
return product_links
示例7: _checkStoredInjections
def _checkStoredInjections(self):
for r in self.results:
# At this state injections in Result obj are not
# compacted yet so it will only be 1st injected param
url, data = r.target.getPayloadedUrl(r.first_param, "")
# In case of proxy
if self.engine.getOption('http-proxy') is not None:
proxy = ProxyHandler({'http': self.engine.getOption('http-proxy')})
opener = build_opener(proxy)
install_opener(opener)
# Some headers
if self.engine.getOption('ua') is not None:
if self.engine.getOption('ua') is "RANDOM":
headers = {'User-Agent': random.choice(USER_AGENTS)}
else:
headers = {'User-Agent': self.engine.getOption('ua')}
else:
headers = {}
if self.engine.getOption("cookie") is not None:
headers["Cookie"] = self.engine.getOption("cookie")
# Build the request
req = Request(url, data, headers)
try:
to = 10 if self.engine.getOption('http-proxy') is None else 20
response = urlopen(req, timeout=to)
except HTTPError, e:
self._addError(e.code, r.target.getAbsoluteUrl())
continue
except URLError, e:
self._addError(e.reason, r.target.getAbsoluteUrl())
continue
示例8: _performInjections
def _performInjections(self, target):
# Check every parameter
for k, v in target.params.iteritems():
pl = Payload(taint=True)
url, data = target.getPayloadedUrl(k, pl.payload)
# In case of proxy
if self.engine.getOption('http-proxy') is not None:
proxy = ProxyHandler({'http': self.engine.getOption('http-proxy')})
opener = build_opener(proxy)
install_opener(opener)
# Some headers
if self.engine.getOption('ua') is not None:
if self.engine.getOption('ua') is "RANDOM":
headers = {'User-Agent': random.choice(USER_AGENTS)}
else:
headers = {'User-Agent': self.engine.getOption('ua')}
else:
headers = {}
if self.engine.getOption("cookie") is not None:
headers["Cookie"] = self.engine.getOption("cookie")
# Build the request
req = Request(url, data, headers)
try:
to = 10 if self.engine.getOption('http-proxy') is None else 20
response = urlopen(req, timeout=to)
except HTTPError, e:
self._addError(e.code, target.getAbsoluteUrl())
return
except URLError, e:
self._addError(e.reason, target.getAbsoluteUrl())
return
示例9: readUrl
def readUrl(inUrl):
tryCount = 0
while tryCount < 5 :
# print "Create CookieJar"
cookies = mechanize.CookieJar()
# print "Build Opener"
opener = mechanize.build_opener(mechanize.HTTPCookieProcessor(cookies))
# print "Add Headers"
opener.addheaders = [("User-agent", "Mozilla/5.0 (compatible; MyProgram/0.1)"),("From", "[email protected]")]
# print "Install Opener"
mechanize.install_opener(opener)
try:
# print "Open URL"
response = mechanize.urlopen(inUrl)
tryCount = 99
except:
tryCount += 1
print "******** Error on urlopen ***********"
print "URL: ", inUrl
print "Trying Again....", tryCount
# print response.read()
# html = urllib.urlopen(inUrl).read()
# print "Reading Response"
html = response.read()
# print "Response Read:", html[0:100]
root = lxml.html.fromstring(html)
# print "Root created: ", root
return root
示例10: test_cookies
def test_cookies(self):
import urllib2
# this test page depends on cookies, and an http-equiv refresh
#cj = CreateBSDDBCookieJar("/home/john/db.db")
cj = CookieJar()
handlers = [
HTTPCookieProcessor(cj),
HTTPRefreshProcessor(max_time=None, honor_time=False),
HTTPEquivProcessor(),
HTTPRedirectHandler(), # needed for Refresh handling in 2.4.0
# HTTPHandler(True),
# HTTPRedirectDebugProcessor(),
# HTTPResponseDebugProcessor(),
]
o = apply(build_opener, handlers)
try:
install_opener(o)
try:
r = urlopen(urljoin(self.uri, "/cgi-bin/cookietest.cgi"))
except urllib2.URLError, e:
#print e.read()
raise
data = r.read()
#print data
self.assert_(
data.find("Your browser supports cookies!") >= 0)
self.assert_(len(cj) == 1)
# test response.seek() (added by HTTPEquivProcessor)
r.seek(0)
samedata = r.read()
r.close()
self.assert_(samedata == data)
示例11: slurp_with_login_and_pwd
def slurp_with_login_and_pwd():
import sys
import mechanize
# sys.path.append('ClientCookie-1.0.3')
# from mechanize import ClientCookie
# sys.path.append('ClientForm-0.1.17')
# import ClientForm
# Create special URL opener (for User-Agent) and cookieJar
cookieJar = mechanize.CookieJar()
opener = mechanize.build_opener(mechanize.HTTPCookieProcessor(cookieJar))
opener.addheaders = [("User-agent","Mozilla/5.0 (compatible)")]
mechanize.install_opener(opener)
fp = mechanize.urlopen("http://login.yahoo.com")
forms = mechanize.ParseResponse(fp)
fp.close()
# print forms on this page
for form in forms:
print "***************************"
print form
form = forms[0]
form["login"] = "yahoo-user-id" # use your userid
form["passwd"] = "password" # use your password
fp = mechanize.urlopen(form.click())
fp.close()
fp = mechanize.urlopen("https://class.coursera.org/ml-003/lecture/download.mp4?lecture_id=1") # use your group
fp.readlines()
fp.close()
示例12: GetHtml
def GetHtml(url):
opener = mechanize.build_opener()
opener.addheaders = [("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0) Gecko/20100101 Firefox/4.0")]
mechanize.install_opener(opener)
request = mechanize.urlopen(url)
html = request.read()
request.close()
return html
示例13: setup_mechanize
def setup_mechanize():
"""
Set up user agent for all mechanize calls.
"""
cookies = mechanize.CookieJar()
opener = mechanize.build_opener(mechanize.HTTPCookieProcessor(cookies))
homepage = "http://github.com/aszlig/picfetcher"
opener.addheaders = [("User-agent", "PicFetcher/0.1.0 (+%s)" % homepage)]
mechanize.install_opener(opener)
示例14: openUrl
def openUrl(url, cookie=None, login=False):
"""
Opens a given url through mechanize.
If there is no cookie (string path) passed in or if there is a cooke path
passed in but the login parameter is False (signifying to open the url with
cookie saved in the cookie path), the html from the opened url is returned
as a string.
If a cookie path is passed in and the login parameter is True, then the
Mechanize.Broswer object is returned to perform a yogaglo login through
a form submission.
"""
browser = mechanize.Browser()
browser.addheaders = [
('User-Agent',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:24.0) Gecko/20100101 Firefox/24.0'),
('Accept',
'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
('Accept-Language', 'en-gb,en;q=0.5'),
('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'),
('Keep-Alive', '115'),
('Connection', 'keep-alive'),
('Cache-Control', 'max-age=0'),
]
#Experimental?
# browser.set_handle_gzip(True)
browser.set_handle_redirect(True)
browser.set_handle_referer(True)
browser.set_handle_robots(False)
browser.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time = 1)
if not cookie is None:
cj = cookielib.LWPCookieJar()
browser.set_cookiejar(cj)
opener = mechanize.build_opener(HTTPCookieProcessor(cj))
mechanize.install_opener(opener)
# trying to login, no cookie, must return browser so it can follow the
# login url
if login is True:
browser.open(url)
return browser
# can't set to expire, can't read when this particular cookie expires
cj.load(cookie , ignore_discard=True)
return browser.open(url).read()
示例15: setUp
def setUp(self):
mechanize._testcase.TestCase.setUp(self)
self.test_uri = urljoin(self.uri, "test_fixtures")
self.server = self.get_cached_fixture("server")
if self.no_proxies:
old_opener_m = mechanize._opener._opener
old_opener_u = urllib2._opener
mechanize.install_opener(mechanize.build_opener(
mechanize.ProxyHandler(proxies={})))
urllib2.install_opener(urllib2.build_opener(
urllib2.ProxyHandler(proxies={})))
def revert_install():
mechanize.install_opener(old_opener_m)
urllib2.install_opener(old_opener_u)
self.add_teardown(revert_install)