XPath Agent: An Efficient XPath Programming Agent Based on LLM for Web Crawler

Yu Li, Bryce Wang, Xinyu LuanarXiv 2025 · DOI

Abstract

Automated web data extraction remains a challenge due to the complexity and volatility of modern HTML structures. We present XPath Agent, a production-ready framework that leverages a two-stage Large Language Model (LLM) pipeline to generate robust XPath queries. By implementing novel HTML sanitization and condensing algorithms, the agent reduces token consumption to 10–20% of the original volume while improving extraction accuracy. Our results on the SWDE dataset show that XPath Agent, powered by GPT-4o, achieves an F1 score of 0.8514, outperforming existing methods in both efficiency and generalizability.

1. Introduction

Web scraping is a cornerstone of business intelligence, yet manual XPath programming is increasingly brittle and time-consuming. While Large Language Models (LLMs) show promise in automating this task, they are often hindered by massive HTML "noise," context window limitations, and a tendency to overfit to specific page structures. XPath Agent addresses these hurdles through a specialized two-stage architecture designed for efficiency and robustness.

2. Methodology

The XPath Agent pipeline separates data identification from logic programming to optimize performance.

2.1 Stage 1: Information Extraction and Sanitization

In the first stage, a lightweight LLM identifies target data and "cue texts"—semantic anchors like "Price:" or "Model:" that act as landmarks. To facilitate this, the raw HTML is processed via an IE HTML Sanitizer. This algorithm strips invisible nodes, empty tags, and non-essential attributes, typically reducing the document size by 80-90%.

2.2 Stage 2: XPath Programming and Synthesis

The second stage utilizes a more capable LLM to generate the final XPath. This model receives a "condensed" HTML snippet containing only the target nodes and their immediate structural context. To ensure generalizability, the agent synthesizes information from three different "seed" pages simultaneously, preventing the creation of brittle, instance-specific queries.

The two-stage XPath Agent pipeline, integrating HTML sanitization, cue text extraction, and iterative programming.

2.3 Conversational Evaluation

A feedback loop, the Conversational XPath Evaluator, tests generated queries against sample pages. If errors occur (e.g., missing or surplus data), the evaluator provides corrective feedback, allowing the agent to refine the XPath over multiple iterations.

3. Experimental Evaluation

The framework was evaluated using the SWDE (Structured Web Data Extraction) dataset, comprising over 20,000 pages across 90 websites. Analysis focused on extraction accuracy and the scaling of token usage relative to raw HTML size. The authors found that sanitized HTML size grows much more slowly than raw page size, keeping costs manageable for large-scale crawls.

4. Results and Discussion

The XPath Agent demonstrated superior performance across multiple LLM backends. GPT-4o emerged as the most effective model, achieving an accuracy of 0.7413 and an F1 score of 0.8514.

Performance comparison of various LLM backends on the SWDE benchmark, highlighting GPT-4o's lead in accuracy and F1 score.

Key insights from the evaluation include:

  • Efficiency through Pruning: Removing HTML noise allows LLMs to operate within context limits and significantly reduces operational costs.
  • Semantic Anchoring: The use of cue texts provides stable landmarks for navigation, even in complex DOM trees.
  • Robustness: Multi-page synthesis effectively mitigates overfitting, producing XPaths that remain valid across entire domains.

5. Conclusion and Future Work

XPath Agent provides a scalable solution for automated web scraping. While highly effective, future work will address failure modes in highly obfuscated dynamic content and explore integration with browser automation tools like Selenium for interactive web environments.

Summary updated

BibTeX
@misc{li2025xpath,
  author = {Yu Li and Bryce Wang and Xinyu Luan},
  title = {{XPath Agent: An Efficient XPath Programming Agent Based on LLM for Web Crawler}},
  year = {2025},
  eprint = {2502.15688},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2502.15688},
}
Figure