Python - arxiv

arxiv


一、关于 arxiv.py


arxiv.py 是 arXiv API 的 Python wrapper。

arXiv API : https://info.arxiv.org/help/api/index.html

arXiv是康奈尔大学图书馆的一个项目,提供物理、数学、计算机科学、定量生物学、定量金融和统计学领域 100 多万篇文章的开放访问。


安装

pip install arxiv

Python 引入

import arxiv


二、使用示例


1、获取结果
import arxiv

# Construct the default API client.
client = arxiv.Client()

# Search for the 10 most recent articles matching the keyword "quantum."
search = arxiv.Search(
  query = "quantum",
  max_results = 10,
  sort_by = arxiv.SortCriterion.SubmittedDate
)

results = client.results(search)

# `results` is a generator; you can iterate over its elements one by one...
for r in client.results(search):
  print(r.title)
# ...or exhaust it into a list. Careful: this is slow for large results sets.
all_results = list(results)
print([r.title for r in all_results])

# For advanced query syntax documentation, see the arXiv API User Manual:
# https://arxiv.org/help/api/user-manual#query_details
search = arxiv.Search(query = "au:del_maestro AND ti:checkerboard")
first_result = next(client.results(search))
print(first_result)

# Search for the paper with ID "1605.08386v1"
search_by_id = arxiv.Search(id_list=["1605.08386v1"])
# Reuse client to fetch the paper, then print its title.
first_result = next(client.results(search))
print(first_result.title)

2、下载 papers

要下载 ID 为 "1605.08386v1 "的论文 PDF,请运行 Search,然后使用 Result.download_pdf()

import arxiv

paper = next(arxiv.Client().results(arxiv.Search(id_list=["1605.08386v1"])))
# Download the PDF to the PWD with a default filename.
paper.download_pdf()
# Download the PDF to the PWD with a custom filename.
paper.download_pdf(filename="downloaded-paper.pdf")
# Download the PDF to a specified directory with a custom filename.
paper.download_pdf(dirpath="./mydir", filename="downloaded-paper.pdf")

同样的界面也可用于下载论文源的 .tar.gz 文件:

import arxiv

paper = next(arxiv.Client().results(arxiv.Search(id_list=["1605.08386v1"])))
# Download the archive to the PWD with a default filename.
paper.download_source()
# Download the archive to the PWD with a custom filename.
paper.download_source(filename="downloaded-paper.tar.gz")
# Download the archive to a specified directory with a custom filename.
paper.download_source(dirpath="./mydir", filename="downloaded-paper.tar.gz")

3、自定义 client 获取结果
import arxiv

big_slow_client = arxiv.Client(
  page_size = 1000,
  delay_seconds = 10.0,
  num_retries = 5
)

# Prints 1000 titles before needing to make another request.
for result in big_slow_client.results(arxiv.Search(query="quantum")):
  print(result.title)

4、日志

要检查此软件包的网络行为和 API 逻辑,请配置一个 DEBUG 级日志记录器。

>>> import logging, arxiv
>>> logging.basicConfig(level=logging.DEBUG)
>>> client = arxiv.Client()
>>> paper = next(client.results(arxiv.Search(id_list=["1605.08386v1"])))
INFO:arxiv.arxiv:Requesting 100 results at offset 0
INFO:arxiv.arxiv:Requesting page (first: False, try: 0): https://export.arxiv.org/api/query?search_query=&id_list=1605.08386v1&sortBy=relevance&sortOrder=descending&start=0&max_results=100
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): export.arxiv.org:443
DEBUG:urllib3.connectionpool:https://export.arxiv.org:443 "GET /api/query?search_query=&id_list=1605.08386v1&sortBy=relevance&sortOrder=descending&start=0&max_results=100&user-agent=arxiv.py%2F1.4.8 HTTP/1.1" 200 979

三、类型说明


1、Client

Client指定了从 arXiv 应用程序接口获取结果的可重用策略。

对于大多数用例,默认client就足够了。

客户端配置指定了分页和重试逻辑。

Reusing客户端允许连续的 API 调用使用相同的连接池,并确保它们遵守您设置的速率限制。


2、Search

Search 指定了对 arXiv 数据库的搜索。

使用Client.results可以获得Results 的生成器。


3、Result

Client.results 生成的 Result 对象包括每篇论文的元数据和下载论文内容的帮助方法。

底层原始数据的含义记录在 arXiv API User Manual: Details of Atom Results Returned

Result 还公开了下载论文的辅助方法: Result.download_pdfResult.download_source.


2024-03-28(四)

### ZTERO-ARXIV-DAILY 资源获取与使用 #### 获取方式 为了获取 `ZTERO-ARXIV-DAILY` 的资源,可以通过 GitHub 上托管的相关项目实现自动化每日抓取 arXiv 论文摘要的功能。具体来说,利用 arXiv 提供的官方 API 接口,结合 GitHub Actions 自动化工作流,能够定时执行脚本并收集最新的研究资料[^1]。 ```yaml name: Fetch ArXiv Papers Daily on: schedule: - cron: '0 7 * * *' # UTC time, adjust as needed jobs: fetch_papers: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v2 - name: Set up Python uses: actions/setup-python@v2 with: python-version: '3.x' - name: Install dependencies run: | pip install requests beautifulsoup4 pandas openpyxl - name: Run script to get papers from arXiv env: ARXIV_API_KEY: ${{ secrets.ARXIV_API_KEY }} run: python fetch_arxiv.py ``` 此 YAML 文件展示了如何配置一个简单的 GitHub Action 工作流来定期调用 arXiv API 并保存所需数据。需要注意的是,在实际应用中应当妥善管理 API 密钥等敏感信息。 #### 使用方法 对于希望基于上述机制构建个人化的 arXiv 日常更新系统的用户而言,除了设置好 Gitub Actions 外部触发条件外,还需要编写相应的 Python 或其他编程语言编写的程序去解析返回的数据,并将其整理成易于阅读的形式存储下来。例如: ```python import requests from bs4 import BeautifulSoup import json def query_arxiv(query_string="quantum computing", start=0, max_results=10): base_url = "http://export.arxiv.org/api/query?" search_query = f"search_query=all:{query_string}&start={start}&max_results={max_results}" response = requests.get(base_url + search_query) soup = BeautifulSoup(response.content, features='xml') entries = [] for entry in soup.find_all('entry'): title = entry.title.text.strip() summary = entry.summary.text.strip().replace('\n', '') entries.append({ 'title': title, 'summary': summary[:500]+'...' if len(summary)>500 else summary }) return entries ``` 这段代码片段提供了一个基本框架用于向 arXiv 发送查询请求并将结果简化为列表形式输出。可以根据需求调整参数以适应不同的应用场景[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程乐园

请我喝杯伯爵奶茶~!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值