本文整理汇总了Python中mechanicalsoup.Browser方法的典型用法代码示例。如果您正苦于以下问题:Python mechanicalsoup.Browser方法的具体用法?Python mechanicalsoup.Browser怎么用?Python mechanicalsoup.Browser使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mechanicalsoup
的用法示例。
在下文中一共展示了mechanicalsoup.Browser方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_submit_set
# 需要导入模块: import mechanicalsoup [as 别名]
# 或者: from mechanicalsoup import Browser [as 别名]
def test_submit_set(httpbin):
"""Complete and submit the pizza form at http://httpbin.org/forms/post """
browser = mechanicalsoup.Browser()
page = browser.get(httpbin + "/forms/post")
form = mechanicalsoup.Form(page.soup.form)
form["custname"] = "Philip J. Fry"
form["size"] = "medium"
form["topping"] = ("cheese", "onion")
form["comments"] = "freezer"
response = browser.submit(form, page.url)
# helpfully the form submits to http://httpbin.org/post which simply
# returns the request headers in json format
json = response.json()
data = json["form"]
assert data["custname"] == "Philip J. Fry"
assert data["custtel"] == "" # web browser submits "" for input left blank
assert data["size"] == "medium"
assert data["topping"] == ["cheese", "onion"]
assert data["comments"] == "freezer"
示例2: _login
# 需要导入模块: import mechanicalsoup [as 别名]
# 或者: from mechanicalsoup import Browser [as 别名]
def _login(self):
login_url = 'https://www.kaggle.com/account/login'
browser = Browser()
login_page = browser.get(login_url)
token = re.search('antiForgeryToken: \'(?P<token>.+)\'', 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<error>.+)"\]', str(login_result_page.soup))
if error_match:
raise Exception('There was an error logging in: ' + error_match.group(1))
return browser
示例3: test_submit_online
# 需要导入模块: import mechanicalsoup [as 别名]
# 或者: from mechanicalsoup import Browser [as 别名]
def test_submit_online(httpbin):
"""Complete and submit the pizza form at http://httpbin.org/forms/post """
browser = mechanicalsoup.Browser()
page = browser.get(httpbin + "/forms/post")
form = mechanicalsoup.Form(page.soup.form)
input_data = {"custname": "Philip J. Fry"}
form.input(input_data)
check_data = {"size": "large", "topping": ["cheese"]}
form.check(check_data)
check_data = {"size": "medium", "topping": "onion"}
form.check(check_data)
form.textarea({"comments": "warm"})
form.textarea({"comments": "actually, no, not warm"})
form.textarea({"comments": "freezer"})
response = browser.submit(form, page.url)
# helpfully the form submits to http://httpbin.org/post which simply
# returns the request headers in json format
json = response.json()
data = json["form"]
assert data["custname"] == "Philip J. Fry"
assert data["custtel"] == "" # web browser submits "" for input left blank
assert data["size"] == "medium"
assert data["topping"] == ["cheese", "onion"]
assert data["comments"] == "freezer"
示例4: test_submit_online
# 需要导入模块: import mechanicalsoup [as 别名]
# 或者: from mechanicalsoup import Browser [as 别名]
def test_submit_online(httpbin):
"""Complete and submit the pizza form at http://httpbin.org/forms/post """
browser = mechanicalsoup.Browser()
page = browser.get(httpbin + "/forms/post")
form = page.soup.form
form.find("input", {"name": "custname"})["value"] = "Philip J. Fry"
# leave custtel blank without value
assert "value" not in form.find("input", {"name": "custtel"}).attrs
form.find("input", {"name": "size", "value": "medium"})["checked"] = ""
form.find("input", {"name": "topping", "value": "cheese"})["checked"] = ""
form.find("input", {"name": "topping", "value": "onion"})["checked"] = ""
form.find("textarea", {"name": "comments"}).insert(0, "freezer")
response = browser.submit(form, page.url)
# helpfully the form submits to http://httpbin.org/post which simply
# returns the request headers in json format
json = response.json()
data = json["form"]
assert data["custname"] == "Philip J. Fry"
assert data["custtel"] == "" # web browser submits "" for input left blank
assert data["size"] == "medium"
assert data["topping"] == ["cheese", "onion"]
assert data["comments"] == "freezer"
assert json["headers"]["User-Agent"].startswith('python-requests/')
assert 'MechanicalSoup' in json["headers"]["User-Agent"]
示例5: test__request
# 需要导入模块: import mechanicalsoup [as 别名]
# 或者: from mechanicalsoup import Browser [as 别名]
def test__request(httpbin):
form_html = """
<form method="post" action="{}/post">
<input name="customer" value="Philip J. Fry"/>
<input name="telephone" value="555"/>
<textarea name="comments">freezer</textarea>
<fieldset>
<legend> Pizza Size </legend>
<p><input type=RADIO name=size value="small">Small</p>
<p><input type=radiO name=size value="medium" checked>Medium</p>
<p><input type=radio name=size value="large">Large</p>
</fieldset>
<fieldset>
<legend> Pizza Toppings </legend>
<p><input type=CHECKBOX name="topping" value="bacon" checked>Bacon</p>
<p><input type=checkBox name="topping" value="cheese">Extra Cheese</p>
<p><input type=checkbox name="topping" value="onion" checked>Onion</p>
<p><input type=checkbox name="topping" value="mushroom">Mushroom</p>
</fieldset>
<select name="shape">
<option value="round">Round</option>
<option value="square" selected>Square</option>
</select>
</form>
""".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"]
示例6: test__request_select_none
# 需要导入模块: import mechanicalsoup [as 别名]
# 或者: from mechanicalsoup import Browser [as 别名]
def test__request_select_none(httpbin):
"""Make sure that a <select> with no options selected
submits the first option, as it does in a browser."""
form_html = """
<form method="post" action={}/post>
<select name="shape">
<option value="round">Round</option>
<option value="square">Square</option>
</select>
</form>""".format(httpbin.url)
form = BeautifulSoup(form_html, "lxml").form
browser = mechanicalsoup.Browser()
response = browser._request(form)
assert response.json()['form'] == {'shape': 'round'}
示例7: test_no_404
# 需要导入模块: import mechanicalsoup [as 别名]
# 或者: from mechanicalsoup import Browser [as 别名]
def test_no_404(httpbin):
browser = mechanicalsoup.Browser()
resp = browser.get(httpbin + "/nosuchpage")
assert resp.status_code == 404
示例8: test_404
# 需要导入模块: import mechanicalsoup [as 别名]
# 或者: from mechanicalsoup import Browser [as 别名]
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
示例9: test_set_cookiejar
# 需要导入模块: import mechanicalsoup [as 别名]
# 或者: from mechanicalsoup import Browser [as 别名]
def test_set_cookiejar(httpbin):
"""Set cookies locally and test that they are received remotely."""
# construct a phony cookiejar and attach it to the session
jar = RequestsCookieJar()
jar.set('field', 'value')
assert jar.get('field') == 'value'
browser = mechanicalsoup.Browser()
browser.set_cookiejar(jar)
resp = browser.get(httpbin + "/cookies")
assert resp.json() == {'cookies': {'field': 'value'}}
示例10: test_get_cookiejar
# 需要导入模块: import mechanicalsoup [as 别名]
# 或者: from mechanicalsoup import Browser [as 别名]
def test_get_cookiejar(httpbin):
"""Test that cookies set by the remote host update our session."""
browser = mechanicalsoup.Browser()
resp = browser.get(httpbin + "/cookies/set?k1=v1&k2=v2")
assert resp.json() == {'cookies': {'k1': 'v1', 'k2': 'v2'}}
jar = browser.get_cookiejar()
assert jar.get('k1') == 'v1'
assert jar.get('k2') == 'v2'
示例11: test_post
# 需要导入模块: import mechanicalsoup [as 别名]
# 或者: from mechanicalsoup import Browser [as 别名]
def test_post(httpbin):
browser = mechanicalsoup.Browser()
data = {'color': 'blue', 'colorblind': 'True'}
resp = browser.post(httpbin + "/post", data)
assert resp.status_code == 200 and resp.json()['form'] == data
示例12: __init__
# 需要导入模块: import mechanicalsoup [as 别名]
# 或者: from mechanicalsoup import Browser [as 别名]
def __init__(self, session, url):
self._session = session
self._browser = mechanicalsoup.Browser(
soup_config=dict(features='html.parser'), session=self._session
)
try:
self._page = self._browser.get(url)
self._page.raise_for_status()
except requests.RequestException as e:
raise GoogleAuthError('Failed to load form: {}'.format(e))
示例13: _get_authorization_code
# 需要导入模块: import mechanicalsoup [as 别名]
# 或者: from mechanicalsoup import Browser [as 别名]
def _get_authorization_code(session, credentials_prompt):
"""Get authorization code using Google account credentials.
Because hangups can't use a real embedded browser, it has to use the
Browser class to enter the user's credentials and retrieve the
authorization code, which is placed in a cookie. This is the most fragile
part of the authentication process, because a change to a login form or an
unexpected prompt could break it.
Raises GoogleAuthError authentication fails.
Returns authorization code string.
"""
browser = Browser(session, OAUTH2_LOGIN_URL)
email = credentials_prompt.get_email()
browser.submit_form(FORM_SELECTOR, {EMAIL_SELECTOR: email})
password = credentials_prompt.get_password()
browser.submit_form(FORM_SELECTOR, {PASSWORD_SELECTOR: password})
if browser.has_selector(TOTP_CHALLENGE_SELECTOR):
browser.submit_form(TOTP_CHALLENGE_SELECTOR, {})
elif browser.has_selector(PHONE_CHALLENGE_SELECTOR):
browser.submit_form(PHONE_CHALLENGE_SELECTOR, {})
if browser.has_selector(VERIFICATION_FORM_SELECTOR):
if browser.has_selector(TOTP_CODE_SELECTOR):
input_selector = TOTP_CODE_SELECTOR
elif browser.has_selector(PHONE_CODE_SELECTOR):
input_selector = PHONE_CODE_SELECTOR
else:
raise GoogleAuthError('Unknown verification code input')
verfification_code = credentials_prompt.get_verification_code()
browser.submit_form(
VERIFICATION_FORM_SELECTOR, {input_selector: verfification_code}
)
try:
return browser.get_cookie('oauth_code')
except KeyError:
raise GoogleAuthError('Authorization code cookie not found')