简体   繁体   English

python,漂亮的汤,xml解析

[英]python, beautiful soup, xml parsing

How can I get values of latitude and longitude from the following XML: 如何从以下XML获取纬度和经度值:

<?xml version="1.0" encoding="utf-8"?>
<location source="FoundByWifi">
<coordinates latitude="49.7926292" longitude="24.0538406" 
nlatitude="49.7935180" nlongitude="24.0552174" />
</location>

I tried to use get_text but it doesn't work in this way( 我尝试使用get_text但这种方式不起作用(

r = requests.get(url)
soup = BeautifulSoup(r.text)
lat = soup.find('coordinates','latitude').get_text(strip=True)

Check online demo 查看在线演示

html_doc = """
<?xml version="1.0" encoding="utf-8"?>
<location source="FoundByWifi">
<coordinates latitude="49.7926292" longitude="24.0538406" 
nlatitude="49.7935180" nlongitude="24.0552174" />
</location>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')
lat = soup.find_all('coordinates')

for i in lat:
  print(i.attrs['latitude'])
  print(i.attrs['longitude'])

'latitude' is an attribute within the 'coordinates' tag. “纬度”是“坐标”标签内的属性。 Once you found the coordinates, the soup object stores all the attributes in a dict-like key-value store. 找到坐标后,汤对象将所有属性存储在类似dict的键值存储中。

So, in your case, after finding the coordinates tag, check the 'latitude' key as so: 因此,在您的情况下,找到坐标标签后,请按以下方式检查“纬度”键:

lat = soup.find('coordinates')['latitude']

You can even strip the resultant of any extraneous whitespace at the beginning or end: 您甚至可以在开头或结尾处剥离任何多余空格的结果:

lat = soup.find('coordinates')['latitude'].strip()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM