python - 如何查询 arXiv 特定年份?

标签 python urllib feedparser

我使用下面所示的代码来从 arXiv 检索论文。我想检索标题中含有“机器”和“学习”一词的论文。论文数量很大,因此我想实现按年份切片(发表)。

如何在 search_query 中请求 2020 年和 2019 年的记录?请注意,我对后过滤不感兴趣。

import urllib.request

import time
import feedparser

# Base api query url
base_url = 'http://export.arxiv.org/api/query?';

# Search parameters
search_query = urllib.parse.quote("ti:machine learning")
start = 0
total_results = 5000
results_per_iteration = 1000
wait_time = 3

papers = []

print('Searching arXiv for %s' % search_query)

for i in range(start,total_results,results_per_iteration):
    
    print("Results %i - %i" % (i,i+results_per_iteration))
    
    query = 'search_query=%s&start=%i&max_results=%i' % (search_query,
                                                         i,
                                                         results_per_iteration)

    # perform a GET request using the base_url and query
    response = urllib.request.urlopen(base_url+query).read()

    # parse the response using feedparser
    feed = feedparser.parse(response)

    # Run through each entry, and print out information
    for entry in feed.entries:
        #print('arxiv-id: %s' % entry.id.split('/abs/')[-1])
        #print('Title:  %s' % entry.title)
        #feedparser v4.1 only grabs the first author
        #print('First Author:  %s' % entry.author)
        paper = {}
        paper["date"] = entry.published
        paper["title"] = entry.title
        paper["first_author"] = entry.author
        paper["summary"] = entry.summary
        papers.append(paper)
    
    # Sleep a bit before calling the API again
    print('Bulk: %i' % 1)
    time.sleep(wait_time)

最佳答案

根据arXiv documentation ,没有可用的 publisheddate 字段。

你能做的就是 sort the results按日期(通过将 &sortBy=subscribedDate&sortOrder=descending 添加到查询参数中)并在到达 2018 年时停止发出请求。

基本上你的代码应该像这样修改:

import urllib.request

import time
import feedparser

# Base api query url
base_url = 'http://export.arxiv.org/api/query?';

# Search parameters
search_query = urllib.parse.quote("ti:machine learning")
i = 0
results_per_iteration = 1000
wait_time = 3
papers = []
year = ""  
print('Searching arXiv for %s' % search_query)

while (year != "2018"): #stop requesting when papers date reach 2018
    print("Results %i - %i" % (i,i+results_per_iteration))
    
    query = 'search_query=%s&start=%i&max_results=%i&sortBy=submittedDate&sortOrder=descending' % (search_query,
                                                         i,
                                                         results_per_iteration)

    # perform a GET request using the base_url and query
    response = urllib.request.urlopen(base_url+query).read()

    # parse the response using feedparser
    feed = feedparser.parse(response)
    # Run through each entry, and print out information
    for entry in feed.entries:
        #print('arxiv-id: %s' % entry.id.split('/abs/')[-1])
        #print('Title:  %s' % entry.title)
        #feedparser v4.1 only grabs the first author
        #print('First Author:  %s' % entry.author)
        paper = {}
        paper["date"] = entry.published
        year = paper["date"][0:4]
        paper["title"] = entry.title
        paper["first_author"] = entry.author
        paper["summary"] = entry.summary
        papers.append(paper)
    # Sleep a bit before calling the API again
    print('Bulk: %i' % 1)
    i += results_per_iteration
    time.sleep(wait_time)

对于“后过滤”方法,一旦收集到足够的结果,我会执行以下操作:

papers2019 = [item for item in papers if item["date"][0:4] == "2019"]

关于python - 如何查询 arXiv 特定年份?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64047299/

相关文章:

Python pandas : Taking values from one database, 为它们分配另一个值并在第二个数据库中替换它们

Python - 语法错误 'Exception'

python - 绘制图像的 PSD 与 x/y 轴

Python 3.4 - 使用标准库的多部分发布

Python:如何使用 feedparser 和 etags 检查 RSS 更新

python - reddit 的 feedparser 返回空

python - Pandas GroupBy 索引

Python 使用 Pandas/Urllib 下载文件

python - 在python中打开txt文件中的链接

Python 3.6 : Feedparser issue getting sub-attributes