Exemple #1
0
def get_xml(url):
    session = XMLSession()
    session.proxies = {'http': HTTP_PROXY, 'https': HTTPS_PROXY}
    retry = Retry(connect=3, backoff_factor=1)
    adapter = HTTPAdapter(max_retries=retry)
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) '
                      'AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.1.2 Safari/603.3.8',
        'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
    session.mount('http://', adapter)
    r = session.get(url, headers=headers, verify=False)
    session.close()

    # results = []
    # if int(status.get('totalRes')) > 0:
    #     for elem in root.findall('{http://purl.org/rss/1.0/}item'):
    #         temp = []
    #         temp.append(elem.find('{http://purl.org/rss/1.0/}title').text)
    #         temp.append(elem.find('{http://purl.org/rss/1.0/}link').text)
    #         results.append(temp)
    return ET.fromstring(r.content)
import os
from functools import partial

import pytest

from requests_xml import XMLSession, AsyncXMLSession, XML
from requests_file import FileAdapter

session = XMLSession()
session.mount('file:///', FileAdapter())


def get():
    path = os.path.sep.join(
        (os.path.dirname(os.path.abspath(__file__)), 'nasa.rss'))
    url = 'file:///{}'.format(path)

    return session.get(url)


@pytest.fixture
def async_get(event_loop):
    """ AsyncSession cannot be created global since it will create
        a different loop from pytest-asyncio. """
    async_session = AsyncXMLSession()
    async_session.mount('file:///', FileAdapter())
    path = os.path.sep.join(
        (os.path.dirname(os.path.abspath(__file__)), 'nasa.rss'))
    url = 'file:///{}'.format(path)

    return partial(async_session.get, url)