BeatifulSoup查找有条件的结果

时间:2019-04-26 12:16:09

标签: python soap web-scraping beautifulsoup

如何用条件解析某些代码?我有肥皂反应,我只需要打印包含type = 1

的component_id
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
    xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <GetTerritoriesHierarhyResponse
            xmlns="http://parsec.ru/Parsec3IntergationService">
            <GetTerritoriesHierarhyResult>
                <Territory xsi:type="TerritoryWithComponent">
                    <ID>13c80b2d-d9d3-47cd-9c11-f80597b61e74</ID>
                    <TYPE>0</TYPE>
                    <NAME>OFFLINE</NAME>
                    <PARENT_ID>88ef0e32-3b6f-467c-a0ec-0733317f6757</PARENT_ID>
                    <COMPONENT_ID>13c80b2d-d9d3-47cd-9c11-f80597b61e74</COMPONENT_ID>
                    <FEATURE_MASK>0</FEATURE_MASK>
                </Territory>
                K>0</FEATURE_MASK>
                </Territory>
<Territory xsi:type="TerritoryWithComponent">
                    <ID>7d432ebb-6199-44c5-b67b-4671718e6e3c</ID>
                    <TYPE>1</TYPE>
                    <NAME>PREO</NAME>
                    <PARENT_ID>88ef0e32-3b6f-467c-a0ec-0733317f6757</PARENT_ID>
                    <COMPONENT_ID>7d432ebb-6199-44c5-b67b-4671718e6e3c</COMPONENT_ID>
                    <FEATURE_MASK>0</FEATURE_MASK>
                </Territory>
            </GetTerritoriesHierarhyResult>
        </GetTerritoriesHierarhyResponse>
    </soap:Body>
</soap:Envelope>

我有一些代码

from bs4 import BeautifulSoup
xml = response.content
soup = BeautifulSoup(xml, 'xml')
for i in soup.find_all('Territory'):
  print(i.text)

响应

13c80b2d-d9d3-47cd-9c11-f80597b61e740OFFLINE88ef0e32-3b6f-467c-a0ec-0733317f675713c80b2d-d9d3-47cd-9c11-f80597b61e740
7d432ebb-6199-44c5-b67b-4671718e6e3c0PREO88ef0e32-3b6f-467c-a0ec-0733317f67577d432ebb-6199-44c5-b67b-4671718e6e3c0

我认为我需要

for i in soup.find_all('Territory'):
  if type = 1 print component_id

2 个答案:

答案 0 :(得分:0)

可以按照if语句的建议进行迭代和检查:

给出:

xml = '''<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
    xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <GetTerritoriesHierarhyResponse
            xmlns="http://parsec.ru/Parsec3IntergationService">
            <GetTerritoriesHierarhyResult>
                <Territory xsi:type="TerritoryWithComponent">
                    <ID>13c80b2d-d9d3-47cd-9c11-f80597b61e74</ID>
                    <TYPE>0</TYPE>
                    <NAME>OFFLINE</NAME>
                    <PARENT_ID>88ef0e32-3b6f-467c-a0ec-0733317f6757</PARENT_ID>
                    <COMPONENT_ID>13c80b2d-d9d3-47cd-9c11-f80597b61e74</COMPONENT_ID>
                    <FEATURE_MASK>0</FEATURE_MASK>
                </Territory>
                K>0</FEATURE_MASK>
                </Territory>
<Territory xsi:type="TerritoryWithComponent">
                    <ID>7d432ebb-6199-44c5-b67b-4671718e6e3c</ID>
                    <TYPE>1</TYPE>
                    <NAME>PREO</NAME>
                    <PARENT_ID>88ef0e32-3b6f-467c-a0ec-0733317f6757</PARENT_ID>
                    <COMPONENT_ID>7d432ebb-6199-44c5-b67b-4671718e6e3c</COMPONENT_ID>
                    <FEATURE_MASK>0</FEATURE_MASK>
                </Territory>
            </GetTerritoriesHierarhyResult>
        </GetTerritoriesHierarhyResponse>
    </soap:Body>
</soap:Envelope>'''

代码:

from bs4 import BeautifulSoup
xml = response.content
soup = BeautifulSoup(xml, 'xml')
for i in soup.find_all('Territory'):
  if i.select('TYPE')[0].text == '1':
      print (i.select('COMPONENT_ID')[0].text)

from bs4 import BeautifulSoup
xml = response.content
soup = BeautifulSoup(xml, 'xml')
for i in soup.find_all('TYPE', text='1'):
    print (i.parent.find('COMPONENT_ID').text)

输出:

7d432ebb-6199-44c5-b67b-4671718e6e3c

答案 1 :(得分:0)

您可以将:has:contains伪类与bs4 4.7.1一起使用以测试是否存在。如果没有,您将得到一个空列表。为解析器指定xml。我认为阅读起来更简洁。

行:

soup = bs(xml, 'xml')
results = [item.select_one('COMPONENT_ID').text for item in soup.select('Territory:has(TYPE:contains("1"))')]
print(results)

完整:

from bs4 import BeautifulSoup as bs

xml = '''
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
    xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <GetTerritoriesHierarhyResponse
            xmlns="http://parsec.ru/Parsec3IntergationService">
            <GetTerritoriesHierarhyResult>
                <Territory xsi:type="TerritoryWithComponent">
                    <ID>13c80b2d-d9d3-47cd-9c11-f80597b61e74</ID>
                    <TYPE>0</TYPE>
                    <NAME>OFFLINE</NAME>
                    <PARENT_ID>88ef0e32-3b6f-467c-a0ec-0733317f6757</PARENT_ID>
                    <COMPONENT_ID>13c80b2d-d9d3-47cd-9c11-f80597b61e74</COMPONENT_ID>
                    <FEATURE_MASK>0</FEATURE_MASK>
                </Territory>
                K>0</FEATURE_MASK>
                </Territory>
                <Territory xsi:type="TerritoryWithComponent">
                    <ID>7d432ebb-6199-44c5-b67b-4671718e6e3c</ID>
                    <TYPE>1</TYPE>
                    <NAME>PREO</NAME>
                    <PARENT_ID>88ef0e32-3b6f-467c-a0ec-0733317f6757</PARENT_ID>
                    <COMPONENT_ID>7d432ebb-6199-44c5-b67b-4671718e6e3c</COMPONENT_ID>
                    <FEATURE_MASK>0</FEATURE_MASK>
                </Territory>
            </GetTerritoriesHierarhyResult>
        </GetTerritoriesHierarhyResponse>
    </soap:Body>
</soap:Envelope>'''

soup = bs(xml, 'xml')
results = [item.select_one('COMPONENT_ID').text for item in soup.select('Territory:has(TYPE:contains("1"))')]
print(results)