Skip to main content
 首页 » 编程设计

python之通过自动化 python 使用边界框按位置查询 wikipedia/mediawiki api

2025年04月02日13zfyouxi

我希望在大范围内获取维基百科文章标题和位置(纬度/经度),对于单个 url 查询来说太大了,例如:

https://en.wikipedia.org/w/api.php?action=query&list=geosearch&gsradius=6000&gscoord=51.967818|-3.290105

或

http://api.geonames.org/wikipediaBoundingBox?north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo

(第二个更好,因为它返回一个边界框,而第一个返回一个点周围的半径,但是它可以在 json 中给出结果(通过添加 '&format=json')而第二个不能)。

如果查询的搜索区域没有限制,或者返回的结果数量没有限制,我就不会有问题。有办法解决这个问题吗?

所以我正在寻求帮助,以找到一种自动执行此过程的好方法,以类似网格的方式对边界框进行大量查询,解析数据(可能使用 python),并将其存储在我的数据库中。

这是我想出的一些代码,但我卡住了:

url = 'http://api.geonames.org/wikipediaBoundingBox?north=%s&south=%s&east=%s&west=%s&username=demo' 
 
data_coords = [ 
{north : 51.990, 51.990, 51.990}, 
 south : 51.917, 51.917, 51.917}, 
 east : -3.247, -3.117, -2.987}, 
 west : -3.377, -3.247, 3.117} 
] 
 
for i in data_coords: 
 
urllib2.urlopen(url % (i['north']), (i['south']), (i['east']), (i['west'])) 

帮助将不胜感激,谢谢!

请您参考如下方法:

我喜欢这个问题。希望这有助于:

def get_grids(area, divisions): 
    if divisions: 
        # left top 
        get_grids([area[0], area[1], area[2]- (area[2] - area[0]) / 2, area[3] - (area[3] - area[1]) / 2], divisions - 1) 
        # right top 
        get_grids([area[0], area[1] + (area[2] - area[1]) / 2, area[2] - (area[2] - area[0]) / 2, area[3]], divisions - 1) 
        # left bottom 
        get_grids([area[0] + (area[2] - area[0]) / 2, area[1], area[2], area[3] - (area[3] - area[1]) / 2], divisions - 1) 
        # right bottom 
        get_grids([area[0] + (area[2] - area[0]) / 2, area[1] + (area[2] - area[1]) / 2, area[2], area[3]], divisions - 1) 
    else: 
        #request area here 
        print(area) 
 
# north, east, south, west 
main_area = [10.0, 10.0, 20.0, 20.0] 
 
get_grids(main_area, 1) 

您必须输入 main_area,这是您的起始区域。之后,您可以在打印件所在的位置进行其余调用。

例如,对于输入:main_area = [10.0, 10.0, 20.0, 20.0]

和2个分区(每个分区是^2)

它输出:

[10.0, 10.0, 12.5, 12.5] 
[10.0, 12.5, 12.5, 15.0] 
[12.5, 10.0, 15.0, 12.5] 
[12.5, 12.5, 15.0, 15.0] 
[10.0, 15.0, 12.5, 17.5] 
[10.0, 15.0, 12.5, 20.0] 
[12.5, 15.0, 15.0, 17.5] 
[12.5, 15.0, 15.0, 20.0] 
[15.0, 10.0, 17.5, 12.5] 
[15.0, 15.0, 17.5, 15.0] 
[17.5, 10.0, 20.0, 12.5] 
[17.5, 15.0, 20.0, 15.0] 
[15.0, 15.0, 17.5, 17.5] 
[15.0, 17.5, 17.5, 20.0] 
[17.5, 15.0, 20.0, 17.5] 
[17.5, 17.5, 20.0, 20.0]