我使用urllib编写了以下函数,但是我想将其更新为仅依赖于获取信息的请求,并使用BeautifulSoup来解析xml数据。这可能吗?
import re
import urllib.request
import urllib.parse
import xml.dom.minidom
def magnetic_variation(coordinates):
rc = []
lat, lon = coordinates
params = urllib.parse.urlencode({'lat1': lat, 'lon1': lon, 'resultFormat': 'xml', 'startMonth': now.month})
url = urllib.request.urlopen("http://www.ngdc.noaa.gov/geomag-web/calculators/calculateDeclination?%s" % params)
dom = xml.dom.minidom.parseString(url.read())
for node in dom.getElementsByTagName("declination")[0].childNodes:
if node.nodeType == node.TEXT_NODE:
rc.append(node.data)
rawdata = ''.join(rc)
variation = str(re.findall(r"[-+]?\d*\.\d+|\d+", rawdata)[0])
return round(float(variation),2)
答案 0 :(得分:0)
import requests, bs4
def magnetic_variation(coordinates):
rc = []
lat, lon = coordinates
params = {'lat1': lat, 'lon1': lon, 'resultFormat': 'xml', 'startMonth': now.month}
url = requests.get("http://www.ngdc.noaa.gov/geomag-web/calculators/calculateDeclination", params=params)
dom = bs4.BeautifulSoup(url.text)
# parse the dom
return round(float(variation),2)