如何使用Python从Wikipedia页面获取坐标?

2024-04-19 02:31:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我想得到给定维基百科页面的坐标。我尝试使用Wikipedia API,但是唯一相关的方法是geosearch(),它返回给定一对坐标的页面,我想要的正好相反


Tags: 方法api页面wikipediageosearch
2条回答

我不确定是否可以使用WikipediaAPI来完成,但是可以很容易地单独完成

import requests
from bs4 import BeautifulSoup as bs
req = requests.get("https://en.wikipedia.org/wiki/Calgary").text
soup = bs(req, 'lxml')
latitude = soup.find("span", {"class": "latitude"})
longitude = soup.find("span", {"class": "longitude"})
print(latitude.text, longitude.text)

您没有提到您正在使用哪个Python库,但一般来说,例如,为了使用Geosearch API获取Washington, D.C.文章的坐标,可以使用以下URL:

https://en.wikipedia.org/w/api.php?action=query&prop=coordinates&titles=Washington,%20D.C.&%20format=json

相关问题 更多 >