Answers for "mechanize python XE #30"

0

mechanize python XE #30

def create_new_account(username, email, password):
    '''It returns True/False.'''

    browser = mechanize.Browser()
    browser.set_handle_robots(False)
    browser.addheaders = [
        ('User-agent',
         'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11'),
        ('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
        ('Accept-Encoding', 'gzip,deflate,sdch'),
        ('Accept-Language', 'en-US,en;q=0.8'),
        ('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.3')
    ]
    browser.set_handle_gzip(True)
    res = browser.open('https://github.com')
    browser.select_form(nr=1)
    browser['user[login]'] = username
    browser['user[email]'] = email
    browser['user[password]'] = password
    s = browser.submit()

    second_page = s.read()
    landing_page_fp = '/tmp/landing-%s.html' % username
    with open(landing_page_fp, 'w') as f:
        f.write(second_page)

    expected_string = 'taken your first step into a larger world, <strong>@%s' % username
    second_page = second_page.decode('ascii', 'ignore')
    if second_page.find(expected_string) >= 0:
        print 'SUCCESS for user %s' % username
        return True
    else:
        # something went wrong
        error_fp = '/tmp/errorpage-%s.html' % username
        with open(error_fp, 'w') as f:
            f.write(second_page)
        print 'Something went wrong. Error page dumped to "%s"' % error_fp
        return False
Posted by: Guest on September-11-2021

Python Answers by Framework

Browse Popular Code Answers by Language