How to use the mechanize.urlopen function in mechanize

To help you get started, we’ve selected a few mechanize examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github python-mechanize / mechanize / test / test_urllib2.py View on Github external
def test_trivial(self):
        # A couple trivial tests

        self.assertRaises(ValueError, mechanize.urlopen, 'bogus url')

        fname = os.path.join(self.make_temp_dir(), "test.txt")
        data = b'data'
        write_file(fname, data)
        if os.sep == '\\':
            fname = '/' + fname
        file_url = "file://" + fname
        try:
            f = mechanize.urlopen(file_url)
        except Exception as e:
            raise ValueError('Failed to open URL: {} for fname: {} with error: {}'.format(file_url, fname, e))
        self.assertEqual(f.read(), data)
        f.close()
github python-mechanize / mechanize / test / test_urllib2_localnet.py View on Github external
def test_200_with_parameters(self):
        expected_response = b'pycon 2008...'
        handler = self._make_request_handler([(200, [], expected_response)])

        f = mechanize.urlopen('http://localhost:%s/bizarre' % handler.port,
                              'get=with_feeling')
        data = f.read()
        f.close()

        self.assertEqual(data, expected_response)
        self.assertEqual(handler.requests, ['/bizarre', b'get=with_feeling'])
github dhruvagarwal / fastsubmit_codechef / fastsubmit.py View on Github external
def recheck(user):
        c=0
        prob_list=mechanize.urlopen('http://www.spoj.com/status/'+user+'/signedlist').read().split('\n')
        for x in prob_list:
                x=x.replace('|',' ').split()
                if len(x)<8 or c<2:
                        if len(x)>7:
                                c+=1
                        continue
                else:
                        return x[4]
github philgyford / mailman-archive-scraper / MailmanArchiveScraper.py View on Github external
def logIn(self):
        """
        Logs in to private archives using the supplied email and password.
        Stores the cookie so we can continue to get subsequent pages.
        """
        
        cookieJar = mechanize.CookieJar()

        opener = mechanize.build_opener(mechanize.HTTPCookieProcessor(cookieJar))
        opener.addheaders = [("User-agent","Mozilla/5.0 (compatible)")]
        mechanize.install_opener(opener)
        
        self.message('Logging in to '+self.list_url)
        fp = mechanize.urlopen(self.list_url)
        forms = ClientForm.ParseResponse(fp, backwards_compat=False)
        fp.close()

        form = forms[0]
        form['username'] = self.username
        form['password'] = self.password
        fp = mechanize.urlopen(form.click())
        fp.close()
github LupusDei / nomaddress.com / lib / dmv_update.py View on Github external
s = TagStripper()
    s.feed(html)
    return s.get_data()

driver_license_number = sys.argv[1]
last_4_digits_ssn = sys.argv[2]
street_address = sys.argv[3]
city = sys.argv[4]
zip_code = sys.argv[5]
county = sys.argv[6]

street_address.replace('"', '').strip()

# Request first page
request = mechanize.Request("https://www.ilsos.gov/addrchange/")
response = mechanize.urlopen(request)
forms = mechanize.ParseResponse(response, backwards_compat=False)
response.close()
form = forms[0]

# Select the correct option, which is Dirvers in our case
form.find_control("updateType").items[0].selected = True
request2 = form.click()

# Request second page
response2 = mechanize.urlopen(request2)
forms2 = mechanize.ParseResponse(response2, backwards_compat=False)
form = forms2[0]

# fill in the main form
form["dlNo"] = driver_license_number
form["last4Ssn"] = last_4_digits_ssn
github matburt / fantasy-tracker / ftracker / yahoo.py View on Github external
for all of the data
        """
        self.cj = mechanize.CookieJar()
        if not self.cookies:
            initialRequest = mechanize.Request(self.url)
            response = mechanize.urlopen(initialRequest)
            forms = ClientForm.ParseResponse(response, backwards_compat=False)
            forms[0]['login'] = self.yahooLogin['user']
            forms[0]['passwd'] = self.yahooLogin['passwd']
            request2 = forms[0].click()
            response2 = mechanize.urlopen(request2)
            self.cj.extract_cookies(response, initialRequest)
            self.cookies = True
        request3 = mechanize.Request(self.url)
        self.cj.add_cookie_header(request3)
        response3 = mechanize.urlopen(request3)
        self.maincontent = response3.read()
        self.soup = BeautifulSoup(self.maincontent)
github daler / ucscsession / ucscsession / tracks.py View on Github external
def __init__(self, url, ucsc_session):
        response = mechanize.urlopen(url)
        self.forms = mechanize.ParseResponse(response, backwards_compat=False)
        self.ucsc_session = ucsc_session
        self.url = url
github LupusDei / nomaddress.com / lib / dmv_update.py View on Github external
street_address.replace('"', '').strip()

# Request first page
request = mechanize.Request("https://www.ilsos.gov/addrchange/")
response = mechanize.urlopen(request)
forms = mechanize.ParseResponse(response, backwards_compat=False)
response.close()
form = forms[0]

# Select the correct option, which is Dirvers in our case
form.find_control("updateType").items[0].selected = True
request2 = form.click()

# Request second page
response2 = mechanize.urlopen(request2)
forms2 = mechanize.ParseResponse(response2, backwards_compat=False)
form = forms2[0]

# fill in the main form
form["dlNo"] = driver_license_number
form["last4Ssn"] = last_4_digits_ssn
form["street"] = street_address
form["city"] = city
form["zipCode"] = zip_code

for item in form.find_control("county").items:
	labels = item.get_labels()
    	if(labels[0].text.lower() == county.lower()):
		item.selected = True
		break
github ruben-gut / freevana / freevana / __init__.py View on Github external
def ajax_request(self, url, params, referer=None):
        """
        Simulate an ajax request
        """
        # Do an Ajax call simulating the browser
        # User mechanize.Request to send POST request
        req = mechanize.Request(url, urllib.urlencode(params))
        req.add_header('User-Agent', HTTP_USER_AGENT)
        req.add_header('X-Requested-With', 'XMLHttpRequest')
        req.add_header('Content-Type', 
                        'application/x-www-form-urlencoded; charset=UTF-8')
        if (referer):
            req.add_header('Referer', referer)
        # Use the same cookie jar we've been using
        self.cookie_jar.add_cookie_header(req)
        result = mechanize.urlopen(req)
        return result.read()