How to use lxml - 10 common examples

To help you get started, we’ve selected a few lxml 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 intel / test-framework-and-suites-for-android / acs / acs / Core / CampaignGenerator / FailedTestCampaignGenerator.py View on Github external
comment = etree.Comment("This Test Campaign was AUTOMATICALLY" +
                                "generated from input ACS report file: %s" %
                                self.failed_folder_path)
        root.insert(0, comment)
        # remove all test cases
        test_cases = root.findall('TestCases')[0]
        test_cases.clear()
        # replace them with the ones in the Test Report
        children = []
        for element in self.all_tc_name:
            if element not in self.failed_tc_names:
                children.append(
                    etree.Comment(
                        etree.tostring(etree.Element('TestCase', Id=element))))
            else:
                children.append(etree.Element('TestCase', Id=element))
        test_cases.extend(children)
        tree.write(self.output_campaign_name)
        XMLUtil.pretty_print_xml(self.output_campaign_name)
        print "Output Failed Tests Campaign can be found here: ", \
            self.output_campaign_name
github django-oscar / django-oscar-mws / tests / unit / test_models.py View on Github external
def test_getting_standard_product_from_upc(self):
        upc = '1234567890'
        profile = factories.AmazonProfileFactory(product__upc=upc)
        spi = profile.get_standard_product_id()
        self.assertEquals(
            etree.tostring(spi),
            PRODUCT_TYPE_XML.format('UPC', upc)
        )
github Digital-Preservation-Finland / dpres-siptools / tests / scripts / test_compile_structmap_ead.py View on Github external
def test_collect_dao_hrefs():
    """Tests that the function collect_dao_hrefs returns a list with
    hrefs without leading slashes from ead3 test data.
    """
    ead3 = (''
            ''
            ''
            '')
    xml = ET.fromstring(ead3)
    hrefs = compile_structmap.collect_dao_hrefs(xml)
    assert hrefs == ['file1.txt', 'file2.txt']
github infinite-Joy / stock-analysis / tests / test_get_stock_data.py View on Github external
from lxml import html
import requests
import time

from get_stock_data import _get_company_primary_stats as get_company_primary_stats
from company_page import CompanyPage

stock_company = "NTPC"
page = requests.get('http://money.rediff.com/%s' % stock_company)
tree = html.fromstring(page.text)
company = CompanyPage(tree)

def test_get_company_primary_stats():
    primary_stats = get_company_primary_stats(company, tree)
    assert primary_stats.get('pe_ratio') > 0
    assert all([primary_stats.get('eps') > 0, primary_stats.get('price_of_stock') > 0, primary_stats.get('fifty_two_wk_high') > 0, primary_stats.get('fifty_two_wk_low') > 0])
github trac-hacks / trac-github / runtests.py View on Github external
# Extract the state from the redirect
                redirect_url = urlparse.urlparse(response.headers['Location'])
                params = urlparse.parse_qs(redirect_url.query, keep_blank_values=True)
                state = params['state'][0]  # this is a random value
                response = session.get(
                    URL + '/github/oauth',
                    params={
                        'code': '01234567890123456789',
                        'state': state
                    },
                    allow_redirects=False)
                self.assertEqual(response.status_code, 302)

                response = session.get(URL + '/prefs')
                self.assertEqual(response.status_code, 200)
                tree = html.fromstring(response.content)
                return (''.join(tree.xpath('//div[@id="warning"]/text()')).strip(),
                        tree.xpath('//input[@id="email"]/@value'))
            finally:
                # disable callback again
                updateMockData(self.mockdata, postcallback="")
github RUB-NDS / DTD-Attacks / code / python / lxml / testLxml.py View on Github external
def testURLInvocation_externalGeneralEntity_attribute_defaults(self):                                    
		#Reset the server back to "0"                                           
		r = requests.get("http://127.0.0.1:5000/reset")                         
		url_counter = "http://127.0.0.1:5000/getCounter"                        
		r = requests.get(url_counter)                                           
		request_content = r.text.replace("\r\n","")                             
		self.assertEqual("0", request_content)  

		parser = XMLParser(attribute_defaults=True) 
		with self.assertRaises(XMLSyntaxError):
			root = parse('../../xml_files_windows/ssrf/url_invocation_externalGeneralEntity.xml',parser)

		#Check if a request has been made                                       
		r = requests.get(url_counter)                                           
		request_content = r.text.replace("\r\n","")                             
		self.assertEqual("0", request_content)
github RUB-NDS / DTD-Attacks / code / python / lxml / testLxml.py View on Github external
def testDOS_entitySize(self):
			parser = XMLParser()
			tree = parse('../../xml_files_windows/dos/dos_entitySize.xml',parser)
			root = tree.getroot()
			count = root.text.count("dos")
			expectedCount = 3400000 
			self.assertEqual(expectedCount, count)
github RUB-NDS / DTD-Attacks / code / python / lxml / testLxml.py View on Github external
def testParameterEntity_doctype_dtd_validation_no_network(self):		
		parser = XMLParser(dtd_validation=True, no_network=False)
		tree = parse('../../xml_files_windows/xxep/parameterEntity_doctype.xml',parser)
		root = tree.getroot()
		self.assertEquals("it_works", root.text)
github kamalgill / flask-appengine-template / src / packages / werkzeug / contrib / testtools.py View on Github external
def lxml(self):
        """Get an lxml etree if possible."""
        if ('html' not in self.mimetype and 'xml' not in self.mimetype):
            raise AttributeError('Not an HTML/XML response')
        from lxml import etree
        try:
            from lxml.html import fromstring
        except ImportError:
            fromstring = etree.HTML
        if self.mimetype=='text/html':
            return fromstring(self.data)
        return etree.XML(self.data)
    lxml = cached_property(lxml)