本文整理匯總了Python中requests.sessions.Session.request方法的典型用法代碼示例。如果您正苦於以下問題:Python Session.request方法的具體用法?Python Session.request怎麽用?Python Session.request使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類requests.sessions.Session
的用法示例。
在下文中一共展示了Session.request方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from requests.sessions import Session [as 別名]
# 或者: from requests.sessions.Session import request [as 別名]
class MeiPai:
def __init__(self):
self.session = Session()
self.session.headers = headers
def search(self, query, search_type='mv', page=1):
query = query.strip()
# topic
if query.startswith('#'):
topic = query.strip('#')
return self.get_videos_by_topic(topic)
cache_exists, result = check_cache_and_return_result(query=query, search_type=search_type, page=page)
if cache_exists:
return result
url = 'http://www.meipai.com/search/{search_type}?'.format(search_type=search_type) + \
urlencode({'q': query, 'page': page})
resp = self.session.request('GET', url)
html = BeautifulSoup(resp.content, 'html.parser')
if search_type == 'mv':
video_links = [div.attrs['data-video'].strip() for div in html.find_all(class_='content-l-video')]
# associated_words = self.word_association(query)
# print("你是否還想搜索:" + ",".join(associated_words))
result = video_links
elif search_type == 'topic':
result = [div.text.strip().strip('#') for div in html.find_all(class_='tcard-name')]
else:
result = []
cache_search_result(query, search_type, page, result)
return result
def get_videos_by_topic(self, topic_name):
"""
get top videos by topic
:param topic_name:
:return:
"""
topic = Topic(topic_name)
topic_id = topic.topic_id
url = "http://www.meipai.com/topics/hot_timeline?page=1&count=24&tid={topic_id}".format(topic_id=topic_id)
resp = self.session.request('GET', url)
result = json.loads(resp.text)
return [media['video'] for media in result['medias']]
# get associated words
def word_association(self, word):
url = 'http://www.meipai.com/search/word_assoc?' + urlencode({'q': word})
resp = self.session.request('GET', url)
return json.loads(resp.text)
示例2: PortalConnection
# 需要導入模塊: from requests.sessions import Session [as 別名]
# 或者: from requests.sessions.Session import request [as 別名]
class PortalConnection(object):
"""
Connection to HubSpot
:param authentication_key: This can be either an :class:`APIKey` or an \
:class:`OAuthKey` instance
:param basestring change_source: The string passed to HubSpot as \
``auditId`` in the query string
"""
_API_URL = 'https://api.hubapi.com'
def __init__(self, authentication_key, change_source):
super(PortalConnection, self).__init__()
self._authentication_handler = \
_QueryStringAuthenticationHandler(authentication_key)
self._change_source = change_source
self._session = Session()
self._session.headers['User-Agent'] = _USER_AGENT
http_adapter = HTTPAdapter(max_retries=_HTTP_CONNECTION_MAX_RETRIES)
self._session.mount('', http_adapter)
def send_get_request(self, url_path, query_string_args=None):
"""
Send a GET request to HubSpot
:param basestring url_path: The URL path to the endpoint
:param dict query_string_args: The query string arguments
:return: Decoded version of the ``JSON`` that HubSpot put in \
the body of the response.
"""
return self._send_request('GET', url_path, query_string_args)
def send_post_request(self, url_path, body_deserialization):
"""
Send a POST request to HubSpot
:param basestring url_path: The URL path to the endpoint
:param dict body_deserialization: The request's body message \
deserialized
:return: Decoded version of the ``JSON`` that HubSpot put in \
the body of the response.
"""
return self._send_request(
'POST',
url_path,
body_deserialization=body_deserialization,
)
def send_put_request(self, url_path, body_deserialization):
"""
Send a PUT request to HubSpot
:param basestring url_path: The URL path to the endpoint
:param body_deserialization: The request's body message deserialized
:return: Decoded version of the ``JSON`` that HubSpot put in \
the body of the response.
"""
return self._send_request(
'PUT',
url_path,
body_deserialization=body_deserialization,
)
def send_delete_request(self, url_path):
"""
Send a DELETE request to HubSpot
:param basestring url_path: The URL path to the endpoint
:return: Decoded version of the ``JSON`` that HubSpot put in \
the body of the response.
"""
return self._send_request('DELETE', url_path)
def _send_request(
self,
method,
url_path,
query_string_args=None,
body_deserialization=None,
):
url = self._API_URL + url_path
query_string_args = query_string_args or {}
query_string_args = dict(query_string_args, auditId=self._change_source)
request_headers = \
{'content-type': 'application/json'} if body_deserialization else {}
if body_deserialization:
request_body_serialization = json_serialize(body_deserialization)
else:
request_body_serialization = None
#.........這裏部分代碼省略.........
示例3: __init__
# 需要導入模塊: from requests.sessions import Session [as 別名]
# 或者: from requests.sessions.Session import request [as 別名]
class Connection:
_API_URL = 'https://www.2degreesnetwork.com/api'
def __init__(self, auth, timeout=None, api_url=None):
super(Connection, self).__init__()
self._api_url = api_url or self._API_URL
self._authentication_handler = auth
self._session = Session()
self._session.headers['User-Agent'] = _USER_AGENT
self._timeout = timeout
http_adapter = HTTPAdapter(max_retries=_HTTP_CONNECTION_MAX_RETRIES)
self._session.mount('', http_adapter)
def send_get_request(self, url, query_string_args=None):
"""
Send a GET request
:param str url: The URL or URL path to the endpoint
:param dict query_string_args: The query string arguments
:return: Decoded version of the ``JSON`` the remote put in \
the body of the response.
"""
return self._send_request('GET', url, query_string_args)
def send_head_request(self, url, query_string_args=None):
"""
Send a HEAD request
:param str url: The URL or URL path to the endpoint
:param dict query_string_args: The query string arguments
"""
return self._send_request('HEAD', url, query_string_args)
def send_post_request(self, url, body_deserialization=None):
"""
Send a POST request
:param str url: The URL or URL path to the endpoint
:param dict body_deserialization: The request's body message \
deserialized
:return: Decoded version of the ``JSON`` the remote put in \
the body of the response.
"""
return self._send_request(
'POST',
url,
body_deserialization=body_deserialization,
)
def send_put_request(self, url, body_deserialization):
"""
Send a PUT request
:param str url: The URL or URL path to the endpoint
:param body_deserialization: The request's body message deserialized
:return: Decoded version of the ``JSON`` the remote put in \
the body of the response.
"""
return self._send_request(
'PUT',
url,
body_deserialization=body_deserialization,
)
def send_delete_request(self, url):
"""
Send a DELETE request
:param str url: The URL or URL path to the endpoint
:return: Decoded version of the ``JSON`` the remote put in \
the body of the response.
"""
return self._send_request('DELETE', url)
def _send_request(
self,
method,
url,
query_string_args=None,
body_deserialization=None,
):
if url.startswith(self._api_url):
url = url
else:
url = self._api_url + url
query_string_args = query_string_args or {}
#.........這裏部分代碼省略.........
示例4: _get_id
# 需要導入模塊: from requests.sessions import Session [as 別名]
# 或者: from requests.sessions.Session import request [as 別名]
def _get_id(self):
session = Session()
resp = session.request('GET', 'http://www.meipai.com/topic/' + quote_plus(self.name),
headers=headers)
html = BeautifulSoup(resp.content, 'html.parser')
return html.find(class_='topic-txt').attrs['data-id']
示例5: MWS
# 需要導入模塊: from requests.sessions import Session [as 別名]
# 或者: from requests.sessions.Session import request [as 別名]
class MWS(object):
""" Base Amazon API class """
# This is used to post/get to the different uris used by amazon per api
# ie. /Orders/2011-01-01
# All subclasses must define their own URI only if needed
URI = "/"
# The API version varies in most amazon APIs
VERSION = "2009-01-01"
# There seem to be some xml namespace issues. therefore every api subclass
# is recommended to define its namespace, so that it can be referenced
# like so AmazonAPISubclass.NS.
# For more information see http://stackoverflow.com/a/8719461/389453
NS = ''
# Some APIs are available only to either a "Merchant" or "Seller"
# the type of account needs to be sent in every call to the amazon MWS.
# This constant defines the exact name of the parameter Amazon expects
# for the specific API being used.
# All subclasses need to define this if they require another account type
# like "Merchant" in which case you define it like so.
# ACCOUNT_TYPE = "Merchant"
# Which is the name of the parameter for that specific account type.
ACCOUNT_TYPE = "SellerId"
def __init__(self, access_key, secret_key, account_id,
domain='https://mws.amazonservices.com', uri="", version=""):
self.access_key = access_key
self.secret_key = secret_key
self.account_id = account_id
self.domain = domain
self.uri = uri or self.URI
self.version = version or self.VERSION
self.session = Session()
bucket_key = getattr(settings, 'RUNSCOPE_BUCKET_KEY', None)
if bucket_key:
logger.info("Redirecting API calls for MWS to runscope")
self.configure_runscope(bucket_key)
def configure_runscope(self, bucket_key):
"""
Configure all connections to be proxied through runscope for easier
debugging and logging of all requests and responses. *bucket_key* is
API for the bucket you want to use for all the request. Check Runscope
for more details on that.
"""
try:
from requests_runscope import RunscopeAdapter
except ImportError:
logger.error(
"Could not import runscope adapter. Is requests-runscope "
"installed? Try running pip install requests-runscope."
)
else:
logger.info(
'Mounting runscope proxy adapter for bucket {}'.format(
bucket_key
)
)
self.session.mount('https://', RunscopeAdapter(bucket_key))
self.session.mount('http://', RunscopeAdapter(bucket_key))
def _get_quote_params(self, params):
quoted_params = []
for key in sorted(params):
value = urllib.quote(
unicode(params[key]).encode('utf-8'),
safe='-_.~'
)
quoted_params.append("{}={}".format(key, value))
return '&'.join(quoted_params)
def make_request(self, extra_data, method="GET", **kwargs):
"""
Make request to Amazon MWS API with these parameters
"""
# Remove all keys with an empty value because
# Amazon's MWS does not allow such a thing.
extra_data = remove_empty(extra_data)
params = {
'AWSAccessKeyId': self.access_key,
self.ACCOUNT_TYPE: self.account_id,
'SignatureVersion': '2',
'Timestamp': self.get_timestamp(),
'Version': self.version,
'SignatureMethod': 'HmacSHA256',
}
params.update(extra_data)
logger.debug("Request Parameters: {}".format(params))
request_description = self._get_quote_params(params)
signature = self.calc_signature(method, request_description)
logger.debug('Domain: {} URI: {}'.format(self.domain, self.uri))
url = '%s%s?%s&Signature=%s' % (self.domain, self.uri,
request_description,
#.........這裏部分代碼省略.........