How to use the mechanicalsoup.Browser function in MechanicalSoup

To help you get started, we’ve selected a few MechanicalSoup 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 MechanicalSoup / MechanicalSoup / tests / test_browser.py View on Github external
def test_404(httpbin):
    browser = mechanicalsoup.Browser(raise_on_404=True)
    with pytest.raises(mechanicalsoup.LinkNotFoundError):
        resp = browser.get(httpbin + "/nosuchpage")
    resp = browser.get(httpbin.url)
    assert resp.status_code == 200
github MechanicalSoup / MechanicalSoup / tests / test_browser.py View on Github external
def test__request_disabled_attr(httpbin):
    """Make sure that disabled form controls are not submitted."""
    form_html = """
    <form action="{}/post" method="post">
      <input value="1" name="nosubmit" disabled="">
    </form>""".format(httpbin.url)

    browser = mechanicalsoup.Browser()
    response = browser._request(BeautifulSoup(form_html, "lxml").form)
    assert response.json()['form'] == {}
github MechanicalSoup / MechanicalSoup / tests / test_browser.py View on Github external
<legend> Pizza Toppings </legend>
        <p><input checked="" value="bacon" name="topping" type="CHECKBOX">Bacon</p>
        <p><input value="cheese" name="topping" type="checkBox">Extra Cheese</p>
        <p><input checked="" value="onion" name="topping" type="checkbox">Onion</p>
        <p><input value="mushroom" name="topping" type="checkbox">Mushroom</p>
      
      <select name="shape">
        <option value="round">Round</option>
        <option selected="" value="square">Square</option>
      </select>
    
    """.format(httpbin.url)

    form = BeautifulSoup(form_html, "lxml").form

    browser = mechanicalsoup.Browser()
    response = browser._request(form)

    data = response.json()['form']
    assert data["customer"] == "Philip J. Fry"
    assert data["telephone"] == "555"
    assert data["comments"] == "freezer"
    assert data["size"] == "medium"
    assert data["topping"] == ["bacon", "onion"]
    assert data["shape"] == "square"

    assert "application/x-www-form-urlencoded" in response.request.headers[
        "Content-Type"]
github drcicero / beautiful-tucan / step1.py View on Github external
def init(inf_cookies: List[str], tuc_cookies: List[str]) -> None:
    pid = mp.current_process().name
    state.dbr = dbm.open(prefix + "cache.db", "r")            # read
    state.dbw = dbm.open(prefix + "cache" + pid + ".db", "n") # write

    state.inferno_br = ms.Browser(soup_config={"features":"lxml"})
    state.tucan_br = ms.Browser(soup_config={"features":"lxml"})
    state.inferno_br.getcached = getcached(state.inferno_br)
    state.tucan_br.getcached = getcached(state.tucan_br)
    for i in inf_cookies: state.inferno_br.get_cookiejar().set_cookie(i)
    for i in tuc_cookies: state.tucan_br.get_cookiejar().set_cookie(i)

    state.pool = None
github realpython / python-basics-exercises / ch16-interacting-with-the-web / 3-interact-with-html-forms.py View on Github external
# 16.3 - Interact with HTML Forms
# Solutions to review exercises

# Make sure BeautifulSoup is installed first with:
# pip3 install MechanicalSoup

import mechanicalsoup


# Exercise 1
browser = mechanicalsoup.Browser()
login_url = "http://olympus.realpython.org/login"
login_page = browser.get(login_url)
login_html = login_page.soup

# select the form and fill in the input fields
form = login_html.form
form.select("input")[0]["value"] = "zeus"
form.select("input")[1]["value"] = "ThunderDude"

# submit form
profiles_page = browser.submit(form, login_page.url)


# Exercise 2
# show profile page title
title = profiles_page.soup.title
github realpython / python-basics-exercises / ch16-interacting-with-the-web / 4-interact-with-websites-in-realtime.py View on Github external
# 16.4 - Interact With Websites in Real-Time
# Solutions to review exercise

# Make sure BeautifulSoup is installed first with:
# pip3 install MechanicalSoup

from time import sleep
import mechanicalsoup

my_browser = mechanicalsoup.Browser()

# Obtain 1 dice roll result every 10 seconds for the next minute
for i in range(0, 6):
    page = my_browser.get("http://olympus.realpython.org/dice")
    html_text = page.soup

    # Return a list of all the tags where the id is 'yfs_184_yhoo'
    dice_result_tag = html_text.select("#result")

    # Take the BeautifulSoup string out of the first tag
    dice_result = dice_result_tag[0].text

    # Grab the timestamp
    time_tag = page.soup.select("#time")
    time = time_tag[0].text
    time = time[: time.find(" - ")]  # Trim string to just the time
github kirkthaker / investopedia-trading-api / InvestopediaApi / ita.py View on Github external
def get_quote(symbol):
    BASE_URL = 'http://www.investopedia.com'
    """
    Returns the Investopedia-delayed price of a given security,
    represented by its stock symbol, a string. Returns false if
    security not found or if another error occurs.
    """
    br = mechanicalsoup.Browser()
    response = br.get(BASE_URL + '/markets/stocks/' + symbol.lower())
    quote_id = "quotePrice"
    parsed_html = response.soup
    try:
        quote = parsed_html.find('td', attrs={'id': quote_id}).text
        quote = quote.replace(",", "")
    except:
        return False
    return float(quote)
github EKami / kaggle-data-downloader / kaggle_data / downloader.py View on Github external
def _login(self):
        login_url = 'https://www.kaggle.com/account/login'
        browser = Browser()

        login_page = browser.get(login_url)
        token = re.search('antiForgeryToken: \'(?P.+)\'', str(login_page.soup)).group(1)
        login_result_page = browser.post(login_url,
                                         data={
                                             'username': self.username,
                                             'password': self.password,
                                             '__RequestVerificationToken': token
                                         })

        error_match = re.search('"errors":\["(?P.+)"\]', str(login_result_page.soup))
        if error_match:
            raise Exception('There was an error logging in: ' + error_match.group(1))

        return browser
github drcicero / beautiful-tucan / step1.py View on Github external
def anonymous_tucan():
    browser = ms.Browser(soup_config={"features":"lxml"})
    page = browser.get(TUCAN_URL)
    page = browser.get(_get_redirection_link(page)) # HTML redirects, because why not
    page = browser.get(_get_redirection_link(page))
    return browser, page