本文整理汇总了Python中mechanize.Browser.set_handle_redirect方法的典型用法代码示例。如果您正苦于以下问题:Python Browser.set_handle_redirect方法的具体用法?Python Browser.set_handle_redirect怎么用?Python Browser.set_handle_redirect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mechanize.Browser
的用法示例。
在下文中一共展示了Browser.set_handle_redirect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_browser
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import set_handle_redirect [as 别名]
def get_browser():
# Browser
br = Browser()
# Cookie Jar
#cj = cookielib.LWPCookieJar()
#br.set_cookiejar(cj)
# Browser options
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
# Follows refresh 0 but not hangs on refresh > 0
#br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
# Want debugging messages?
#
#br.set_debug_http(True)
#br.set_debug_redirects(True)
#br.set_debug_responses(True)
# User-Agent (this is cheating, ok?)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
return br
示例2: get_browser
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import set_handle_redirect [as 别名]
def get_browser(self):
"""
Each FAUrl object stores it's own browser instance. On the first call
it is created and if the username and password is set it will
authenticate you.
:return: mechanize.Browser instance.
:raise: FAiler.FAError if FA is down. Time to F5!
:raise: FAiler.FAAuth Your username and password failed
"""
if self._br is None:
br = Browser()
br.set_handle_robots(False)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_equiv(True)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
if self._username is not None and self._password is not None:
loginPage = 'https://www.furaffinity.net/login'
try:
br.open(loginPage)
except urllib2.HTTPError:
raise FAError("FA's down, F5 time.")
br.form = br.global_form()
br.form['name'] = self._username
br.form['pass'] = self._password
br.form.method = 'POST'
br.submit()
if br.geturl() == loginPage + '/?msg=1':
raise FAAuth('Username & Password Incorrect')
self._br = br
return self._br
示例3: find_first_article
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import set_handle_redirect [as 别名]
def find_first_article():
mech = Browser()
cj = cookielib.LWPCookieJar()
mech.set_handle_equiv(True)
# mech.set_handle_gzip(True)
mech.set_handle_redirect(True)
mech.set_handle_referer(True)
mech.set_handle_robots(False)
# mech.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
mech.addheaders = [
(
"User-agent",
"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1",
)
]
page = mech.open("https://bitcointalk.org/index.php?board=77.0")
html = page.read()
soup = BeautifulSoup(html)
first_article_tag = soup.find("td", class_="windowbg")
global startingpost
startingpost = first_article_tag.span.a.get("href")
print startingpost
示例4: scrape
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import set_handle_redirect [as 别名]
def scrape(self):
"""
Opens the html page and parses the pdf links.
"""
browser = Browser()
#-----------
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
values1 = {'name' : 'Michael Foord',
'location' : 'Northampton',
'language' : 'Python' }
headers = { 'User-Agent' : user_agent }
browser.set_handle_redirect(True)
browser.set_handle_referer(True)
browser.set_handle_robots(False)
browser.addheaders = [('User-Agent', 'Firefox')]
#-------------
browser.set_handle_robots(False)
html = browser.open(self.site)
lines = html.read().splitlines()
for line in lines:
urls = re.findall('<a href="?\'?([^"\'>]*)', line)
for url in urls:
if '.pdf"' in url:
self.pdf_urls.append(url)
示例5: extract_article_url
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import set_handle_redirect [as 别名]
def extract_article_url(posturl):
mech = Browser()
cj = cookielib.LWPCookieJar()
mech.set_handle_equiv(True)
# mech.set_handle_gzip(True)
mech.set_handle_redirect(True)
mech.set_handle_referer(True)
mech.set_handle_robots(False)
# mech.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
mech.addheaders = [
(
"User-agent",
"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1",
)
]
page = mech.open(posturl)
html = page.read()
global soup
soup = BeautifulSoup(html)
global articleURL
# print soup.prettify()
for item in soup.find_all("div", class_="post"):
for link in item.find_all("a"):
string = link.get("href")
if prog.match(string):
# find the link that is to the article (link outside of bitcointalk.org forum)
articleURL = link.get("href")
return link.get("href")
return "No article url"
示例6: createbrowser
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import set_handle_redirect [as 别名]
def createbrowser(self):
br = Browser()
br.set_handle_gzip(True)
br.set_handle_robots(False)
br.set_handle_redirect(True)
br.addheaders = [('User-agent', 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1 like Mac OS X; en-US) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B179 Safari/7534.48.3')]
return br
示例7: __init__
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import set_handle_redirect [as 别名]
def __init__(self, api_key, api_secret, frob, userid, password, perms):
from mechanize import Browser
browser = Browser()
browser.set_handle_robots(False)
browser.set_handle_refresh(True, max_time=1)
browser.set_handle_redirect(True)
api_sig = ''.join((api_secret, 'api_key', api_key, 'frob', frob, 'perms', perms))
api_sig = md5(api_sig)
login_url = "http://flickr.com/services/auth/?api_key=%s&perms=%s&frob=%s&api_sig=%s" % (api_key, perms, frob, api_sig)
browser.open(login_url)
browser.select_form(name='login_form')
browser['login'] = userid
browser['passwd'] = password
browser.submit()
for form in browser.forms():
try:
if form['frob'] == frob:
browser.form = form
browser.submit()
break
except:
pass
else:
raise Exception('no form for authentication found') # lame :-/
示例8: authenticate
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import set_handle_redirect [as 别名]
def authenticate(self):
if self._client:
return self._client
try:
client = Browser()
client.set_handle_redirect(True)
client.set_handle_robots(False)
client.open('http://%s/cgi-bin/videoconfiguration.cgi' % self.camera.host)
client.select_form('frmLOGON')
client['LOGIN_ACCOUNT'] = self.camera.username
client['LOGIN_PASSWORD'] = self.camera.password
client.submit()
try:
client.select_form('frmLOGON')
except FormNotFoundError:
pass
else:
raise AccessDenied('Access denied for user `%s`' % self.camera.username)
except AccessDenied:
raise
except Exception, e:
raise ImproperlyConfigured(e.message)
示例9: browserInit
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import set_handle_redirect [as 别名]
def browserInit():
'''
Set standard, initial browser configuration.
'''
browser = Browser()
browser.set_handle_equiv(True)
browser.set_handle_redirect(True)
browser.set_handle_referer(True)
browser.set_handle_robots(False)
browser.addheaders = [('user-agent', ' Mozilla/5.0 (X11; U; Linux x86_64; en-US) Mechanize/0.2.4 Fedora/16 (Verne) Pytane/0.2'),
('accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')]
return browser
示例10: init_browser
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import set_handle_redirect [as 别名]
def init_browser():
browser = Browser()
browser.addheaders = (
('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
('User-agent', 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)') # We're Firefox! :P
)
# browser.set_handle_gzip(True) # Currently experimental in mechanize
browser.set_handle_redirect(True)
browser.set_handle_refresh(False)
browser.set_handle_robots(True)
browser.set_handled_schemes(['http', 'https'])
browser.set_proxies({})
return browser
示例11: lockNloadBrowser
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import set_handle_redirect [as 别名]
def lockNloadBrowser():
br = Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
# Follows refresh 0 but not hangs on refresh > 0
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
return br
示例12: __init__
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import set_handle_redirect [as 别名]
class Api:
appid = '5415093'
token = None
query_pattern = 'https://api.vk.com/method/%s?%s&access_token='
ua = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1'
br = None
cl = None
def __init__(self, login, password, scope, testmode=False):
self.br = Browser()
self.cl = cookielib.LWPCookieJar()
self.br.set_cookiejar(self.cl)
self.br.set_handle_equiv(True)
self.br.set_handle_redirect(True)
self.br.set_handle_referer(True)
self.br.set_handle_robots(False)
self.br.set_handle_refresh(_http.HTTPRefreshProcessor(), max_time=1)
self.br.addheaders = [('User-agent', self.ua)]
self.br.open('https://oauth.vk.com/authorize?client_id=' + self.appid +
'&scope=' + scope + '&redirect_uri=http://oauth.vk.com/blank.html' +
'&display=mobile&response_type=token')
self.br.select_form(nr=0)
self.br.form['email'] = login
self.br.form['pass'] = password
self.br.submit()
if len(list(self.br.forms())) > 0:
self.br.select_form(nr=0)
self.br.submit()
params = urlparse.urlparse(self.br.geturl()).fragment
params = params.split('&')
for val in params:
tp = val.split('=')
if tp[0] == 'access_token':
self.token = tp[1]
self.query_pattern += self.token
if testmode:
self.query_pattern += '&test_mode=1'
break
def query(self, func, data):
response = self.br.open(self.query_pattern % (func, data))
return response.read()
示例13: Crawler
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import set_handle_redirect [as 别名]
class Crawler():
def __init__(self):
self.br = Browser()
self.br.set_handle_redirect(True)
self.queue_server = {"host": "localhost", "port": PORT}
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
self.sock.connect((self.queue_server['host'], self.queue_server['port']))
except socket.error, (value, message):
print "Could not connect to queue server at " + self.queue_server['host'] +\
":" + str(self.queue_server['port'])
print message
return
示例14: create_browser
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import set_handle_redirect [as 别名]
def create_browser(debug=False):
browser = Browser(factory=mechanize.RobustFactory())
if debug:
# Maybe enable this if you want even more spam...
# logger = logging.getLogger("mechanize")
# logger.addHandler(logging.StreamHandler(sys.stdout))
# logger.setLevel(logging.DEBUG)
browser.set_debug_http(True)
browser.set_debug_responses(True)
browser.set_debug_redirects(True)
browser.set_handle_equiv(True)
browser.set_handle_gzip(True)
browser.set_handle_redirect(True)
browser.set_handle_referer(True)
browser.set_handle_robots(False)
browser.addheaders = HEADERS
return browser
示例15: login
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import set_handle_redirect [as 别名]
def login(self):
br = Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.set_handle_refresh(_http.HTTPRefreshProcessor(), max_time=2)
br.open('http://www.fatsecret.com/Auth.aspx?pa=s')
br.select_form(nr=0)
#name attr of login tr
#PLEASE input your username and password here!!!!
br['_ctl0:_ctl7:Logincontrol1:Name'] = 'username'
br['_ctl0:_ctl7:Logincontrol1:Password'] = 'password'
br.submit()
return br