여러 태그 텍스트를 기반으로 상위 태그 찾기
다음과 같이 파일에 xml의 일부가 있다고 생각하십시오.
<Client name="Jack">
<Type>premium</Type>
<Usage>unlimited</Usage>
<Payment>online</Payment>
</Client>
<Client name="Jill">
<Type>demo</Type>
<Usage>limited</Usage>
<Payment>online</Payment>
</Client>
<Client name="Ross">
<Type>premium</Type>
<Usage>unlimited</Usage>
<Payment>online</Payment>
</Client>
값을 구문 분석하기 위해 BeautifulSoup을 사용하고 있습니다.
여기에서 태그를 기반으로 클라이언트 이름을 가져와야합니다. 태그의 텍스트를 기반으로 클라이언트 이름을 가져와야합니다 (부모 태그에서).
다음과 같은 기능이 있습니다.
def get_client_for_usage(self, usage):
"""
To get the client name for specified usage
"""
usage_items = self.parser.findAll("client")
client_for_usage = []
for usages in usage_items:
try:
client_set = usages.find("usage", text=usage).findParent("client")
client_attr = dict(client_set.attrs)
client_name = client_attr[u'name']
client_for_usage.append(client_name)
except AttributeError:
continue
return client_for_usage
이제 클라이언트 이름을 가져와야하지만 사용 및 유형을 기반으로하는 두 가지를 기반으로합니다.
그래서 클라이언트 이름을 얻을 수 있도록 유형과 사용법을 모두 전달해야합니다.
어떤 사람은 저를 똑같이 도와줍니다. 질문이 명확하지 않은 경우 필요에 따라 수정할 수 있도록 알려주세요.
뭔가
def get_client_for_usage(self, usage, tpe):
"""
To get the client name for specified usage
"""
usage_items = self.parser.findAll("client")
client_for_usage = []
for usages in usage_items:
try:
client_set = usages.find("usage", text=usage).findParent("client")
typ_node = usages.find("type", text=tpe).findParent("client")
if client_set == typ_node:
client_for_usage.append(client_set['name'])
except AttributeError:
continue
return client_for_usage
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다