我有一个包含 500 种濒危物种的数据框,我希望从中收集数字数据。我正在遍历它们的学名,因为这是最一致的命名做法,并检索了 5 年内每个物种的网页浏览量。
但是,当我使用 Panthera tigris 进行查询时,我收到的浏览量非常低,只有 300-4000+,而当我使用 Tiger 进行查询时,它们的浏览量超过 700,000。我知道 Tiger 的官方维基百科页面标题为 Tiger,而不是 Panthera tigris。所以我认为我需要将重定向作为参数包含在内,但是,不知道在 API 请求中的何处包含重定向?并且会简单地将重定向作为参数返回给我目标页面的查看次数吗?而不是重定向页面?
我不能只用通用名称来调用,因为不可能(没有人工检查,我没有时间对 500 多个物种进行检查)知道一个物种页面的标题是哪个通用名称,或者它们是否是页面以他们的科学名称为标题。
我的请求行的示例。此示例中不包含用户代理。
def WikiPageView(name):
# Calling monthly page views of each species
address = "https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/en.wikipedia.org/all-access/user/" + name + "/monthly/2015010100/2020123100"
headers = CaseInsensitiveDict()
headers["Accept"] = "application/json"
# Personal username for identification for the Wikipedia API
headers = {'User-Agent': 'username/0.00 ([email protected])'}
resp = requests.get(address, headers=headers)
details = resp.json()
return details
# Loop over the pd series of species names and call the wikipage function
for name in species['scientific_name']:
# Spaces are replaced with an underscore for the wikipedia API
name = name.replace(" ", "_")
result = WikiPageView(name)
在交互式 PageViews Analysis 网站上搜索每月的页面浏览量会导致一些我似乎也无法弄清楚的不一致。我已标记为在设置和搜索查询中包含重定向,但与普通名称相比,仅使用学名搜索给我的浏览量要低得多。
当我搜索Tiger时,2020年3月有700,000次浏览量的峰值。
这些应该显示相同的结果不是吗?由于 Panthera tigris 重定向到 Tiger。然而,最重要的是,当你一起搜索这些术语时,Panthera tigris 突然返回了与 Tiger 几乎相同的页面浏览量和峰值,这是以前没有的。
有人知道这些不一致的原因吗?或者知道如何将重定向包含到 API 请求中?
我认为解决这个问题的最佳方法是对维基百科的 API 使用 python 包装器,并通过首先使用每个物种的科学名称搜索它来检索每个页面的 url。返回的 url 将包含每个物种的标题,然后可以在 Pageviews API 中使用。
import wikipediaapi
# only english language pages
api = wikipediaapi.Wikipedia('en')
wikiurls = []
# loop through the unique species names
for name in species['scientific_name'].unique():
# Spaces to be replaced with underscore for Wiki's API
name = name.replace(" ", "_")
p = api.page(name)
# Store the url retrieved and name we called with
data = {'url': p.fullurl, 'scientific_name': name}
# Append dictionary to list
wikiurls.append(data)
for n in range(len(wikiurls)):
title = wikiurls[n]['url'].removeprefix("https://en.wikipedia.org/wiki/")
result = WikiPageView(title)