【发布时间】:2020-02-20 06:01:30
【问题描述】:
我正在尝试从两个具有相同名称的节点捕获数据。我正在使用 find 方法,但它总是提取嵌套的值,而不是第一个子节点。我尝试了几种不同的目标方法,但没有任何成功。一如既往地感谢任何帮助。
API Response:
<affiliate_export_response>
<success>true</success>
<row_count>1</row_count>
<affiliates>
<blacklists>
<blacklist>
<advertiser>
<advertiser_id xmlns="API:id_name_store">2</advertiser_id>
<advertiser_name xmlns="API:id_name_store">wayne's Ad</advertiser_name>
</advertiser>
<affiliate>
<affiliate_id xmlns="API:id_name_store">3</affiliate_id>
<affiliate_name xmlns="API:id_name_store">Mark Affiliate</affiliate_name>
</affiliate>
<blacklist_reason>
<blacklist_reason_id xmlns="API:id_name_store">1</blacklist_reason_id>
<blacklist_reason_name xmlns="API:id_name_store">404</blacklist_reason_name>
</blacklist_reason>
<blacklist_type>
<blacklist_type_id xmlns="API:id_name_store">3</blacklist_type_id>
<blacklist_type_name xmlns="API:id_name_store">404</blacklist_type_name>
</blacklist_type>
<date_created>2018-04-26T00:00:00</date_created>
</blacklist>
</blacklists>
<date_created>2018-01-29T11:40:58.34</date_created>
<notes />
</affiliate>
</affiliates>
</affiliate_export_response>
Code:
import requests
from bs4 import BeautifulSoup
url = 'API URL'
params = {
'param1':'dfasdf',
'param2':3
}
r = requests.get(url, params=params)
soup = BeautifulSoup(r.text, 'lxml')
for affiliate in soup.select('affiliate'):
date_created = affiliate.find('date_created').string
print(date_created)
目标是捕获 2018-01-29T11:40:58.34,但我正在捕获黑名单中的嵌套 date_created 节点并获取 2018-04-26T00:00:00。
【问题讨论】:
标签: python python-3.x xml beautifulsoup