当前位置: 首页>>代码示例>>Python>>正文


Python Browser.open方法代码示例

本文整理汇总了Python中mechanize.Browser.open方法的典型用法代码示例。如果您正苦于以下问题:Python Browser.open方法的具体用法?Python Browser.open怎么用?Python Browser.open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在mechanize.Browser的用法示例。


在下文中一共展示了Browser.open方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _process

# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import open [as 别名]
    def _process(self):
        """Start the work."""
        movie = "+".join(self.title.split())
        br = Browser()
        url = "%s/find?s=tt&q=%s" % (self.BASE_URL, movie)
        br.open(url)

        if re.search(r"/title/tt.*", br.geturl()):
            self.url = "%s://%s%s" % urlparse.urlparse(br.geturl())[:3]
            soup = BeautifulSoup(MyOpener().open(url).read(), "html.parser")
        else:
            link = br.find_link(url_regex=re.compile(r"/title/tt.*"))
            res = br.follow_link(link)
            self.url = urlparse.urljoin(self.BASE_URL, link.url)
            soup = BeautifulSoup(res.read(), "html.parser")

        try:
            self.title = soup.find("h1").contents[0].strip()
            for span in soup.findAll("span"):
                if span.has_attr("itemprop") and span["itemprop"] == "ratingValue":
                    self.rating = span.contents[0]
                    break
            self.found = True
        except:
            pass

        self.genre = []
        infobar = soup.find("div", {"class": "infobar"})
        r = infobar.find("", {"title": True})["title"]
        self.genrelist = infobar.findAll("a", {"href": True})

        for i in range(len(self.genrelist) - 1):
            self.genrelist[i] = self.genrelist[i].encode("ascii")
            self.genre.append(self.genrelist[i][16 : self.genrelist[i].index("?")])
        self.mainGenre = self.genre[0]
开发者ID:samcmuldroch,项目名称:m158a-node_python_odot,代码行数:37,代码来源:imbd.py

示例2: url_handler

# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import open [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()
开发者ID:christian91cruz,项目名称:Twitter-Search-Engine-and-Data-Retrieval,代码行数:35,代码来源:gatherTweets.py

示例3: GetPIN

# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import open [as 别名]
class GetPIN():
    def __init__(self,url,username, password):
        self.br = Browser()
        self.br.set_handle_equiv(False)
        self.br.set_handle_robots(False)
        self.url = url
        self.username = username
        self.password = password
 
    def getPIN(self):
        self.br.open(self.url)
        try:
            self.br.select_form(name="authZForm")
            self.br['userId'] = self.username
            self.br['passwd'] = self.password
            response = self.br.submit()
            data = response.readlines()
        except:
            data = self.br.response().readlines()
        pattern = r'<span class="fb">(.*?)</span>' 
        pat = re.compile(pattern)
        for line in data:
            if pat.search(line):
                verifier = pat.findall(line)
                break
        if len(verifier):
            return verifier[0]
        else:
            return -1
开发者ID:chengjun,项目名称:Research,代码行数:31,代码来源:getPIN.py

示例4: __init__

# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import open [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 :-/
开发者ID:pezam,项目名称:Cohen,代码行数:28,代码来源:flickr_storage.py

示例5: get_browser

# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import open [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
开发者ID:onlyhavecans,项目名称:FAiler,代码行数:34,代码来源:faurl.py

示例6: GetXboxLiveFriends

# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import open [as 别名]
  def GetXboxLiveFriends(self):
    """Return a list of tuples (gamer_tag, gamer_presence)."""
    br = Browser()
    br.open('http://live.xbox.com/en-US/profile/Friends.aspx')
    br.select_form(name='f1')
    br['login'] = self.login
    br['passwd'] = self.passwd
    br.submit()  # Submit login form.
    br.select_form(name='fmHF')
    response = br.submit()  # Submit redirect form.
    friend_list = response.read()
    response.close()

    soup = BeautifulSoup(friend_list)
    friend_table = soup.find('table', {'class': FRIEND_TABLE_CLASS})
    if friend_table is None:
      raise XboxLiveError('Parsing failure.')

    friends = []
    for row in friend_table.contents[1:]:  # Skip header row.
      gamer_tag = row.find('td', {'class': GAMER_TAG_CLASS})
      gamer_tag = str(gamer_tag.find('a').contents[0])
      gamer_presence = row.find('td', {'class': GAMER_PRESENCE_CLASS})
      gamer_presence = str(gamer_presence.find('h4').contents[0])
      friends.append((gamer_tag, gamer_presence))
    return friends
开发者ID:damonkohler,项目名称:pypert,代码行数:28,代码来源:xbox_live.py

示例7: _process

# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import open [as 别名]
    def _process(self):
        """Start the work."""
        movie = '+'.join(self.title.split())
        br = Browser()
        url = "%s/find?s=tt&q=%s" % (self.BASE_URL, movie)
        br.open(url)

        if re.search(r'/title/tt.*', br.geturl()):
            self.url = "%s://%s%s" % urlparse.urlparse(br.geturl())[:3]
            soup = BeautifulSoup( MyOpener().open(url).read() )
        else:
            link = br.find_link(url_regex = re.compile(r'/title/tt.*'))
            res = br.follow_link(link)
            self.url = urlparse.urljoin(self.BASE_URL, link.url)
            soup = BeautifulSoup(res.read())

        try:
            self.title = soup.find('h1').contents[0].strip()
            for span in soup.findAll('span'):
                if span.has_key('itemprop') and span['itemprop'] == 'ratingValue':
                    self.rating = span.contents[0]
                    break
            self.found = True
        except:
            pass
开发者ID:abhinavgupta,项目名称:IMDB-rating,代码行数:27,代码来源:IMDB.py

示例8: login

# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import open [as 别名]
def login(url):
    # Use mechanize to get the set name URLs to scrape
    br = Browser()
    br.addheaders = [('User-Agent', ua)]
    br.open(url)

    # Select the form
    for form in br.forms():
        if form.attrs['id'] == 'loginFrm':
            br.form = form
            break

    br["email"] = EMAIL # replace with email
    br["password"] = PASSWORD # replace with password

    # Submit the form
    br.submit()

    for form in br.forms():
        if form.attrs['id'] == 'pop_report_form':
            br.form = form
            break

    br['sport_id'] = ['185223']
    br['set_name'] = "T206"
    br.submit(name="search")

    # Follow link to the correct set
    br.follow_link(url="http://www.beckett.com/grading/set_match/3518008")

    return br.response().read()
开发者ID:jdesilvio,项目名称:T206db,代码行数:33,代码来源:bvgScrape.py

示例9: test_external_groups_visibility_groupspage

# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import open [as 别名]
    def test_external_groups_visibility_groupspage(self):
        """webgroup - external group visibility in groups page"""
        browser = Browser()
        browser.open(CFG_SITE_SECURE_URL + "/youraccount/login")
        browser.select_form(nr=0)
        browser['p_un'] = 'admin'
        browser['p_pw'] = ''
        browser.submit()

        expected_response = "You are logged in as admin"
        login_response_body = browser.response().read()
        try:
            login_response_body.index(expected_response)
        except ValueError:
            self.fail("Expected to see %s, got %s." % \
                      (expected_response, login_response_body))

        browser.open(CFG_SITE_SECURE_URL + "/yourgroups/display")
        expected_response = self.goodgroup
        groups_body = browser.response().read()
        try:
            groups_body.index(expected_response)
        except ValueError:
            self.fail("Expected to see %s, got %s." % \
                    (expected_response, groups_body))

        not_expected_response = self.badgroup
        try:
            groups_body.index(not_expected_response)
        except ValueError:
            pass
        else:
            self.fail("Not expected to see %s, got %s." % \
                    (not_expected_response, groups_body))
开发者ID:pombredanne,项目名称:invenio,代码行数:36,代码来源:webgroup_regression_tests.py

示例10: fetch_laws_page_from_year

# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import open [as 别名]
def fetch_laws_page_from_year(year, temporaryDirectory):  
    lawsDirectory = os.path.join(temporaryDirectory, 'all_laws');
    if not os.path.exists(lawsDirectory):
        os.makedirs(lawsDirectory)
        print('The laws directory did not exist so I created it')
        print(lawsDirectory)

    fileToWriteLawsListIn = os.path.join(lawsDirectory, year + '.html')
    print('File to write in is ' + fileToWriteLawsListIn)
    lawWasNotDownloaded = not os.path.isfile(fileToWriteLawsListIn)
    if lawWasNotDownloaded:
        startDownload = int(round(time.time() * 1000))
        
        print('Getting laws from year ' + year)
        url = get_ugly_url_for_laws(year)
        browser = Browser()
        browser.open(url)
        html = browser.response().get_data()

        with open(fileToWriteLawsListIn, 'a') as f: 
            f.write (html)

        endDownload = int(round(time.time() * 1000))
        print('Finished downloading laws for year ' + year + '. It took only ' 
              + str(endDownload - startDownload) + ' milliseconds')
    else:
        print('This year was already fetched ' + year 
              + '. Skipping to the next year')
开发者ID:andreicristianpetcu,项目名称:pubdocs,代码行数:30,代码来源:1_clrro_fetch_laws_per_year.py

示例11: login_to_kaggle

# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import open [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
开发者ID:joostgp,项目名称:ml_toolbox,代码行数:34,代码来源:kaggle.py

示例12: returnMnemonics

# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import open [as 别名]
def returnMnemonics(var):
    from mechanize import Browser
    from bs4 import BeautifulSoup
    # var = "abase"
    br = Browser()
    br.set_handle_robots(False)
    br.set_handle_equiv(False)
    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')]
    url= 'http://mnemonicdictionary.com/?word=' + str(var)
    br.open(url)

    soup_mn = BeautifulSoup(br.response().read())

    # <div style="padding-top: 10px;">
    count_mn=0
    mnemonics=""
    for i in soup_mn.find_all('div',{'style':'padding-top: 10px;'}):

        soup2 = BeautifulSoup(str(i))
        for x in soup2.find_all('div', {'class':'row-fluid'}):
            soup3 = BeautifulSoup(str(x))

            for y in soup3.find_all('div', {'class':'span9'}):
                count = 0
                # print count_mn
                if count_mn==3:
                    break
                count_mn = count_mn+1
                if y is not None:
                    for z in y:
                        if count == 2:
                            # print z
                            mnemonics = mnemonics+z.strip().replace(','," ").replace('\n', '').replace(".","")+","
                        count = count+1
    return mnemonics
开发者ID:amukho14,项目名称:WordScrape,代码行数:37,代码来源:getMnemonics.py

示例13: authenticate

# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import open [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)
开发者ID:Rudy24,项目名称:modnoemesto,代码行数:28,代码来源:axis.py

示例14: login_and_authorize

# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import open [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."
开发者ID:racktivity,项目名称:ext-pylabs-core,代码行数:27,代码来源:helpers.py

示例15: exec_

# 需要导入模块: from mechanize import Browser [as 别名]
# 或者: from mechanize.Browser import open [as 别名]
def exec_(config, edition, another_edition, script):
    USERNAME = config['USERNAME']
    PASSWORD = config['PASSWORD']
    SCRIPT_ID = config[edition]['SCRIPT_ID']

    LOGIN_URL = 'https://greasyfork.org/users/sign_in'
    EDIT_URL = 'https://greasyfork.org/scripts/{0}/versions/new'.format(SCRIPT_ID)

    summary = make_summary()
    another_edition = config[another_edition]
    another_edition = 'https://greasyfork.org/scripts/{0}-{1}'.format(another_edition['SCRIPT_ID'], another_edition['SCRIPT_NAME'])
    summary = summary.getResult(edition, another_edition)

    b = Browser()

    # login
    b.open(LOGIN_URL)
    b.select_form(nr=3)
    b['user[email]'] = USERNAME
    b['user[password]'] = PASSWORD
    b.submit()

    # edit source
    b.open(EDIT_URL)
    b.select_form(nr=2)
    b['script_version[additional_info]'] = summary.encode('utf-8')
    b['script_version[code]'] = script.encode('utf-8')
    b.submit(name='commit')
开发者ID:JavierBardem,项目名称:adsbypasser,代码行数:30,代码来源:greasyfork.py


注:本文中的mechanize.Browser.open方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。