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

2024-06-15 09:23:05 发布

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

我希望获得Wikipedia文章标题和位置(lat/long)的范围很大,对于单个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']))

感谢您的帮助,谢谢!你知道吗


Tags: orgapijsonhttpurldatademousername
1条回答
网友
1楼 · 发布于 2024-06-15 09:23:05

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

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)

你必须输入主区域,这是你的起始区域。在那之后,你可以在打印的地方调用rest。你知道吗

例如,对于输入: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]

相关问题 更多 >