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

标签 python json xml mediawiki mediawiki-api

我希望在大范围内获取维基百科文章标题和位置(纬度/经度),对于单个 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]

关于python - 通过自动化 python 使用边界框按位置查询 wikipedia/mediawiki api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37327628/

相关文章:

javascript - Flask 从 PushState 接收 url 更改

c# - 如何从相对路径加载c#中的json文件?

android - 如何只设置圆圈大小(在xml中)?

javascript - 想要从字符串中删除花括号

android - 如何将 utf-16 字符放入 Android 字符串资源中?

xml - yyyy-mm-dd 格式的日期不会抛出错误

python - 如何动态地将css应用到django include

python - 打印二叉树

Pythonnet异常: "The ' ExeConfigFilename' argument cannot be null"

javascript - 这个 JSON 字符串有什么问题?它还可以与 JSONLint 一起使用