本文整理汇总了Python中mechanize.Browser.title方法的典型用法代码示例。如果您正苦于以下问题:Python Browser.title方法的具体用法?Python Browser.title怎么用?Python Browser.title使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mechanize.Browser
的用法示例。
在下文中一共展示了Browser.title方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: login_and_authorize
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import title [as 别名]
def login_and_authorize(argv=None):
if argv is None:
argv = sys.argv
authorize_url = argv[1]
config = json.loads(argv[2])
print "AUTHORIZING", authorize_url
br = Browser()
br.set_debug_redirects(True)
br.open(authorize_url)
print "FIRST PAGE", br.title(), br.geturl()
br.select_form(nr=2)
br["login_email"] = config[u"testing_user"]
br["login_password"] = config[u"testing_password"]
resp = br.submit()
print "RESULT PAGE TITLE", br.title()
print "RESULT URL", resp.geturl()
assert br.viewing_html(), "Looks like it busted."
try:
br.select_form(nr=2)
br.submit()
assert br.viewing_html(), "Looks like it busted."
assert "API Request Authorized" in br.title(), "Title Is Wrong (bad email/password?): %r at %r" % (br.title(), br.geturl())
except FormNotFoundError:
print "Looks like we're blessed."
return "OK"
示例2: login_and_authorize
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import title [as 别名]
def login_and_authorize(authorize_url, config):
print "AUTHORIZING", authorize_url
br = Browser()
br.set_debug_redirects(True)
br.open(authorize_url)
print "FIRST PAGE", br.title(), br.geturl()
br.select_form(nr=1)
br['login_email'] = config['testing_user']
br['login_password'] = config['testing_password']
resp = br.submit()
print "RESULT PAGE TITLE", br.title()
print "RESULT URL", resp.geturl()
assert br.viewing_html(), "Looks like it busted."
try:
br.select_form(nr=1)
br.submit()
br.viewing_html(), "Looks like it busted."
assert "API Request Authorized" in br.title(), "Title Is Wrong (bad email/password?): %r at %r" % (br.title(), br.geturl())
except FormNotFoundError:
print "Looks like we're blessed."
示例3: login_to_kaggle
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import title [as 别名]
def login_to_kaggle(self):
""" Login to Kaggle website
Parameters:
-----------
None
Returns:
browser: Browser
a mechanizer Browser object to be used for further access to site
"""
if self.verbose:
print("Logging in to Kaggle..."),
br = Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
br.open(self.kag_login_url)
br.select_form(nr=0)
br['UserName'] = self.kag_username
br['Password'] = self.kag_password
br.submit(nr=0)
if br.title() == "Login | Kaggle":
raise KaggleError("Unable to login Kaggle with username %s (response title: %s)" % (self.kag_username,br.title()))
if self.verbose:
print("done!")
return br
示例4: main
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import title [as 别名]
def main():
# Command line options.
parser = optparse.OptionParser('usage: %prog [options]')
parser.add_option('--unread', dest='unread', action='store_true',
default=False, help='Only tag unread items')
(opts, args) = parser.parse_args()
# Get all items.
api = readitlater.API(configs.RIL_APIKEY, configs.RIL_USERNAME,
configs.RIL_PASSWORD)
items = api.get(state=('unread' if opts.unread else None))
list = items['list']
br = Browser()
# Iterate over items.
for k, v in list.items():
#if v['title'] == v['url']:
if not v['title']:
print u'Found: {0} ({1})'.format(v['title'], v['url']);
try:
doc = br.open(v['url']);
except urllib2.HTTPError, e:
print u'Error fetching page: {0}'.format(e.code)
continue
if not br.viewing_html():
print u'Not a HTML file!'
else:
title = br.title();
print u'New title: {0}'.format(title.decode('ascii', 'ignore'))
# Send the new tags to RIL.
api.send(update_title=[{'url': v['url'], 'title': title}])
示例5: url_handler
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import title [as 别名]
def url_handler(data, lock):
tweet = json.loads(data)
if tweet['entities']['urls']:
for index in range(len(tweet['entities']['urls'])):
url = tweet['entities']['urls'][index]['expanded_url']
try:
tweet['entities']['urls'][index]['url_title'] = "-"
br = Browser()
br.open(url)
title = br.title()
if title == None:
title = "-"
tweet['entities']['urls'][index]['url_title'] = title.encode('ascii','ignore')
except (BrowserStateError, ParseError, UnicodeDecodeError, URLError):
pass
else:
tweet
lock.acquire()
try:
try:
global count
f = open('json/tweets-' + str(count) + '.json', 'a')
if f.tell() >= 9900000:
f.close()
count += 1
f = open('json/tweets-' + str(count) + '.json', 'a')
f.write(json.dumps(tweet) + "\n")
f.close()
except UnicodeDecodeError, e:
pass
finally:
lock.release()
示例6: populate
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import title [as 别名]
def populate(filename, page_category):
file_path = urls_dir + filename
page_urls = open(file_path, 'r')
next_url = page_urls.readline()[:-1]
cat = Category.objects.get(category = page_category)
# Set up browser object. robots.txt causes problems, so it is disabled.
br = Browser()
br.set_handle_robots(False)
# While the URLs file still has URLs
while next_url != "":
# Check if the given webpage still exists. If so, pull out the webpage <title> element and store it.
try:
br.open(next_url, timeout=5)
exists = True
title = safe_unicode(br.title())
if title is None:
title = 'Untitled page'
print "Found: " + title
# Exceptions to handle 403, 404, 503 errors and pages that cannot be parsed
except HTTPError, e:
print 'HTTP Error: ' + str(e)
exists = False
except URLError, e:
print 'URL Error: ' + str(e)
exists = False
示例7: fetch
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import title [as 别名]
def fetch():
br = Browser() # Create a browser
map = {};
# br.open(login_url) # Open the login page
# br.select_form(id="signform") # Find the login form
# br['username'] = username # Set the form values
# br['password'] = password
# resp = br.submit() # Submit the form
br.open('http://www.verycd.com/sto/music/china/')
nice_links = [l for l in br.links()
if 'topics' in l.url]
if not nice_links:
return None
for link in nice_links:
if link.url in map.keys():
continue
try:
response = br.follow_link(link)
map[link.url] = br.title()
except Exception, e:
print >> sys.stderr, e
示例8: get_title
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import title [as 别名]
def get_title(self, url):
'''
Return the title of the passed
in URL.
'''
br = Browser()
br.set_handle_robots(False)
br.open(url)
return 'Title: {}'.format(br.title())
示例9: main
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import title [as 别名]
def main():
cj = cookielib.LWPCookieJar('cookie.txt')
try:
cj.load('cookie.txt')
except IOError:
print "Кукисы не найдены"
root_url = 'http://188.134.114.45/store1/admin/'
admin_url ='http://188.134.114.45/store1/admin/index.php?route=common/home&token='
suppler_url = "http://188.134.114.45/store1/admin/index.php?route=catalog/suppler/update&token="
orinon1_url = '&suppler_id=1'
orinon2_url = '&suppler_id=2'
username = 'admin'
userpass = '1q2w'
br = Browser()
br.set_handle_robots(False)
br.set_cookiejar(cj)
br.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.6')]
home_page = br.open(root_url)
print br.title()
br.select_form(nr=0)
br["username"] = username
br["password"] = userpass
br.submit()
token = getToken(br.geturl())
cj.save('cookie.txt')
br.open(suppler_url + token + orinon1_url)
br.select_form(nr = 1)
control = br.form.find_control("command")
control.set_value_by_label(("Удалить товары",))
res = br.submit()
br.open(suppler_url + token + orinon2_url)
br.select_form(nr = 1)
control = br.form.find_control("command")
control.set_value_by_label(("Удалить товары",))
res = br.submit()
示例10: find_title
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import title [as 别名]
def find_title(self, url):
"""
This finds the title when provided with a string of a URL.
"""
br = Browser()
br.open(url)
title = br.title()
if(title):
return title
else:
return False
示例11: fetch
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import title [as 别名]
def fetch():
result_no = 0 # Number the output files
br = Browser() # Create a browser
br.open(LOGIN_URL) # Open the login page
br.select_form(name="login") # Find the login form
br['username'] = USERNAME # Set the form values
br['password'] = PASSWORD
resp = br.submit() # Submit the form
# Automatic redirect sometimes fails, follow manually when needed
if 'Redirecting' in br.title():
resp = br.follow_link(text_regex='click here')
# Loop through the searches, keeping fixed query parameters
for actor in in VARIABLE_QUERY:
# I like to watch what's happening in the console
print >> sys.stderr, '***', actor
# Lets do the actual query now
br.open(SEARCH_URL + FIXED_QUERY + actor)
# The query actually gives us links to the content pages we like,
# but there are some other links on the page that we ignore
nice_links = [l for l in br.links()
if 'good_path' in l.url
and 'credential' in l.url]
if not nice_links: # Maybe the relevant results are empty
break
for link in nice_links:
try:
response = br.follow_link(link)
# More console reporting on title of followed link page
print >> sys.stderr, br.title()
# Increment output filenames, open and write the file
result_no += 1
out = open(result_%04d % result_no, 'w')
print >> out, response.read()
out.close()
# Nothing ever goes perfectly, ignore if we do not get page
except mechanize._response.httperror_seek_wrapper:
print >> sys.stderr, "Response error (probably 404)"
# Let's not hammer the site too much between fetches
time.sleep(1)
示例12: inferTitleFromURL
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import title [as 别名]
def inferTitleFromURL(self, url):
from mechanize import Browser
from urlparse import urlparse
try:
result = urlparse(url)
if result.scheme not in ['http', 'https']:
return None
browser = Browser()
browser.open(url)
return unicode(browser.title(), 'utf8')
except Exception as e:
NSLog("Exception: " + str(e))
return None
示例13: login_and_authorize
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import title [as 别名]
def login_and_authorize(self, authorize_url):
from mechanize import Browser, FormNotFoundError
import getpass
print "AUTHORIZING", authorize_url
br = Browser()
br.set_debug_redirects(True)
br.open(authorize_url)
print "FIRST PAGE", br.title(), br.geturl()
br.select_form(nr=1)
br['login_email'] = raw_input('Enter your dropbox email: ')
br['login_password'] = getpass.getpass('Enter your dropbox password: ')
resp = br.submit()
print "RESULT PAGE TITLE", br.title()
print "RESULT URL", resp.geturl()
assert br.viewing_html(), "Looks like it busted."
try:
br.select_form(nr=1)
br.submit()
assert br.viewing_html(), "Looks like it busted."
assert "API Request Authorized" in br.title(), "Title Is Wrong (bad email/password?): %r at %r" % (br.title(), br.geturl())
except FormNotFoundError:
print "Looks like we're blessed."
示例14: get_title
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import title [as 别名]
def get_title(self):
if self.title is not None:
return self.title
b = Browser()
site = ".".join(map(lambda x: x.capitalize(), self.url.split("/")[2].replace("www.", "").split(".")[:-1]))
try:
b.open(self.url)
return "[%s] %s" % (site.encode("Utf-8"), encoding_sucks(b.title()))
except URLError:
self.title = "[%s] Error: couldn't fetch the title" % site
self.save()
return self.title
示例15: __init__
# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import title [as 别名]
class Lockerz:
def __init__( self, name, password ):
self.name = name
self.password = password
self.br = Browser()
def connect( self ):
self.br.open( "http://www.lockerz.com" )
self.br.select_form( nr=0 )
self.br["handle"] = self.name
self.br["password"] = self.password
self.br.submit()
return "Lockerz : My Locker" in self.br.title()
def answer_all( self, generator, recursive=False ):
page = self.br.open( "http://www.lockerz.com/dailies" );
self._answer_all( page, generator )
# ..
if recursive:
i = 0
while True:
try:
i+=1
page = self.br.follow_link( text_regex="< Previous Posts" )
print "-- page %d" % i
self._answer_all( page, generator )
except LinkNotFoundError:
break
def answer( self, id, answer ):
d = urllib.urlencode( { "id": id, "a": answer, "o": None } )
r = self.br.open( "http://www.lockerz.com/daily/answer", d );
print r.read()
self.br.back()
def getPTZ( self ):
s = BeautifulSoup( self.br.open( "http://www.lockerz.com" ).read() )
return s.find( "span", attrs={ "class": "ptz_value" } ).string
def _answer_all( self, page, generator ):
s = BeautifulSoup( page.read() )
e = s.findAll( "div", attrs={ "class": re.compile( "dailiesEntry dailyIndex*" ) } )
for i in e:
try:
self.answer( i["id"], generator.getRandomSentence() )
except KeyError:
print "Already answered ..."