arxiv.py Python Wrapper

repository·master·Indexed 22 days ago

https://github.com/lukasschwab/arxiv.py

A Python wrapper for the official arXiv API that provides an interface for searching, fetching metadata, and downloading papers. The library uses a three-part pattern consisting of the Client for connection and rate-limit management, the Search class for query definition, and Result objects for accessing article metadata and PDF/source URLs.

Tokens
1.7K
Snippets
5
Records
12
Agent score
32%

What's inside arxiv.py

  1. How Client, Search, and Result work together

    master

    The library follows a three-part pattern:

    1. Client: The engine. It manages connection pooling, rate limiting (via delay_seconds), and pagination logic. You should create one client and reuse it for multiple searches.
    2. Search: The query definition. It encapsulates what you are looking for (keywords, IDs, sort order) without actually performing the network request.
    3. Result: The data. These are the objects yielded by the client's generator. They contain metadata (title, authors, etc.) and URLs for downloading the paper content.
  2. Debug network behavior with logging

    master

    To inspect API requests, pagination, and network activity, configure the standard Python logging module to the DEBUG level.

    import logging, arxiv
    logging.basicConfig(level=logging.DEBUG)
    client = arxiv.Client()
    # Subsequent calls will now log INFO and DEBUG messages
  3. Download paper PDFs and source files

    master

    Once you have a Result object, you can access its URLs to download files using standard libraries like urllib.request.

    • Use paper.pdf_url for the PDF link.
    • Use paper.source_url() for the source tarball link.
    import arxiv
    from urllib.request import urlretrieve
    
    paper = next(arxiv.Client().results(arxiv.Search(id_list=["1605.08386v1"])))
    
    # Download the PDF.
    urlretrieve(paper.pdf_url, "paper.pdf")
    
    # Download the source tarball.
    urlretrieve(paper.source_url(), "paper.tar.gz")
  4. Configure a custom arxiv.Client

    master

    The arxiv.Client defines the strategy for fetching results, including pagination and retry logic. Reusing a client instance is recommended to benefit from connection pooling and consistent rate limiting.

    Customizable parameters include:

    • page_size: Number of results to request per page.
    • delay_seconds: Delay between requests to respect rate limits.
    • num_retries: Number of retries for failed requests.
    import arxiv
    
    big_slow_client = arxiv.Client(
      page_size = 1000,
      delay_seconds = 10.0,
      num_retries = 5
    )
    
    for result in big_slow_client.results(arxiv.Search(query="quantum")):
      print(result.title)
  5. Search for articles using arxiv.Search

    master

    To find papers, construct an arxiv.Search object and pass it to an arxiv.Client.results() method.

    arxiv.Search supports:

    • query: A search string (e.g., "quantum" or advanced syntax like "au:del_maestro AND ti:checkerboard").
    • max_results: The maximum number of results to return.
    • sort_by: Sorting criteria using arxiv.SortCriterion (e.g., arxiv.SortCriterion.SubmittedDate).
    • id_list: A list of specific arXiv IDs to fetch (e.g., ["1605.08386v1"]).

    client.results(search) returns a generator that yields Result objects. You can iterate over them one by one or convert them to a list using list(results) (note: converting to a list can be slow for large result sets).

    import arxiv
    
    client = arxiv.Client()
    search = arxiv.Search(
      query = "quantum",
      max_results = 10,
      sort_by = arxiv.SortCriterion.SubmittedDate
    )
    
    results = client.results(search)
    for r in results:
      print(r.title)
  6. Use the Client class to fetch arXiv results

    master

    The Client class is the primary way to interact with the arXiv API. It manages pagination, retries, and rate-limiting (respecting arXiv's request frequency guidelines). Use Client.results(search) to get an iterator of Result objects.

    Key configuration options for Client:

    • page_size: Maximum results per API request (default: 100, API limit is 2000).
    • delay_seconds: Seconds to wait between requests (default: 3.0, recommended to follow arXiv's 3-second rule).
    • num_retries: Number of retries for failed requests (default: 3).
  7. Configure a Search with Search class

    master

    The Search class defines the criteria for an arXiv query. You can search by keyword, limit by specific IDs, and specify sorting.

    Parameters:

    • query: An unencoded query string (e.g., au:del_maestro AND ti:checkerboard).
    • id_list: A list of specific arXiv article IDs to limit the search to.
    • max_results: The maximum number of results to return. Set to None to fetch all available results (up to the API limit of 300,000).
    • sort_by: A SortCriterion (e.g., Relevance, LastUpdatedDate, SubmittedDate).
    • sort_order: A SortOrder (Ascending or Descending).
  8. Access metadata from a Result object

    master

    Each Result object represents an arXiv article and provides several attributes:

    • entry_id: The full URL (e.g., https://arxiv.org/abs/2107.05580v1).
    • get_short_id(): Returns the identifier without the URL prefix (e.g., 2107.05580v1).
    • title: The article title.
    • authors: A list of Result.Author objects.
    • summary: The article abstract.
    • published: The original publication datetime.
    • updated: The last update datetime.
    • primary_category: The primary arXiv category.
    • categories: A list of all associated categories.
    • pdf_url: The URL to the PDF version (if available).
    • source_url(): Returns a URL for the source tarfile (replaces /pdf/ with /src/ in the PDF URL).
  9. Handle Result authors and links

    master

    A Result contains nested objects for authors and links:

    Authors (Result.Author)

    • name: The author's name.
    • affiliation: A list of affiliation strings (if provided by arXiv).

    Links (Result.Link)

    • href: The URL.
    • title: The link's title (e.g., "pdf").
    • rel: The relationship to the result.
  10. Handle arXiv API errors

    master

    The library defines a hierarchy of exceptions for handling issues during API interaction:

    • ArxivError: The base exception for the package.
    • HTTPError: Raised when a non-200 status code is encountered. Includes the status code.
    • UnexpectedEmptyPageError: Raised when a page that should contain results is empty (often due to arXiv API brittleness). Includes the raw_feed for diagnostics.
    • MissingFieldError: Raised if an entry is unparseable due to missing required fields.
  11. Sort arXiv results using SortCriterion and SortOrder

    master

    When constructing a Search object, use the following enums to control result ordering:

    SortCriterion

    • Relevance: Sort by query relevance.
    • LastUpdatedDate: Sort by the last time the entry was updated.
    • SubmittedDate: Sort by the original submission date.

    SortOrder

    • Ascending
    • Descending