Skip to main content
Extract web data with AI agents using Zenrows’ enterprise-grade scraping infrastructure. The langchain-zenrows integration enables large language models (LLMs) to access real-time web data using Zenrows’ robust scraping infrastructure. This guide covers how to scrape data with LLMs using the langchain-zenrows module.

What is LangChain?

LangChain is a framework that connects large language models to external data sources and applications. It provides a composable architecture that enables you to create AI workflows by chaining LLM operations, from simple prompt-response patterns to autonomous agents. One key advantage of LangChain is that it allows for easy swapping, coupling, and decoupling of LLMs.

Key Benefits of Integrating LangChain With Zenrows

The langchain-zenrows integration brings the following benefits:
  • Integrate Zenrows with LLMs: Easily integrate scraping capabilities into your desired LLM.
  • Build an agentic data pipeline: Assign different data pipeline roles to each LLM agent based on its capabilities.
  • Real-time web access without getting blocked: Fetch live web content without antibot or JavaScript rendering limitations.
  • Multiple output formats: Fetch website data in various formats, including HTML, Markdown, Plaintext, PDF, or Screenshots.
  • Specific data point extraction: Extract specific data from web pages, such as emails, tables, phone numbers, images, and more.
  • Support for custom parsing: Fetch specific information from web elements using Zenrows’ advanced CSS selector feature.

Use Cases

Here are some use cases of the langchain-zenrows integration:
  • Real-time monitoring: Develop an AI application that scrapes and monitors website content changes in real-time.
  • Market research and demand forecasting: Scrape demand signals, such as reviews, social comments, engagement metrics, price trends, and more. Then, pass the data to an LLM model for forecasting.
  • Finding the best deals: Spot the best deals for a specific product from several e-commerce websites using Zenrows.
  • Review summarization: Summarize scraped reviews using a selected model.
  • Sentiment analysis: Scrape and analyze sentiment in social comments or product reviews.
  • Product research and comparison: Compare products across multiple retail websites and e-commerce platforms to identify the best options.
  • Consistent data pipeline update: Keep your data pipeline up to date with fresh data by integrating langchain-zenrows into your pipeline operations.

Getting Started: Basic Usage

Let’s start with a simple example that uses the langchain-zenrows package to scrape the Antibot Challenge page and return its content in Markdown format. Install the langchain-zenrows package using pip:
Import the ZenRowsUniversalScraper class from the langchain_zenrows module, instantiate the universal scraper with your Zenrows API key, and specify Zenrows parameters with the response_type set to markdown:
Python
The integration bypasses the target site’s antibot measure and returns its content as Markdown:
Output
You’ve successfully integrated Zenrows with LangChain and bypassed an antibot challenge. Let’s build an AI research assistant with this integration.

Advanced Usage: Building an AI Research Assistant

Let’s take things a step further by building an AI-powered pricing research assistant for Etsy. Using the langchain-zenrows integration together with OpenAI’s gpt-4o-mini model, our assistant will automatically visit Etsy’s accessories category and extract key product details such as names, prices, and URLs. Here’s the prompt we’ll use to guide the assistant:

Example Prompt

Prompt

Step 1: Install the packages

Step 2: Add Zenrows as a scraping tool for the AI model

Import the necessary modules and define your Zenrows and OpenAI API keys. Instantiate OpenAI’s chat model and langchain-zenrows integration with the relevant API keys. Configure the LLM agent to use Zenrows as a scraping tool:
Python

Step 3: Prompt the AI Agent

Invoke the AI agent with the research prompt and execute the scraper. As stated in the prompt, the agent uses Zenrows’ markdown response to scrape the target page in Markdown format. It then analyzes the result and returns the 4 cheapest products:
Python
The agent uses Zenrows to visit and scrape the product information. Once scraped, the agent returns the items in the desired format.

Complete Code Example

Combine the snippets from the two steps, and you’ll get the following code:
Python
The above code returns the names, prices, and URLs of the 4 cheapest products in JSON format as expected.

Example Output

Output
Congratulations! 🎉 You’ve now integrated Zenrows as a web scraping tool for an AI agent using the langchain-zenrows module.

API Reference

For complete parameter documentation and details, see the official Zenrows API Reference.

Troubleshooting

Token limit exceeded

  • Solution 1: If you hit the LLM token limit, it means the output size has exceeded what the model can process in a single request. You can parse specific data and then feed it to the LLM model.
  • Solution 2: If the issue is related to usage-based token quotas or the model version’s capabilities, consider upgrading your plan or switching to a higher model with higher bandwidth. For instance, moving from gpt-3.5 to gpt-4o-mini increases the token limit significantly.

API key error

  • Solution 1: Ensure you’ve added your Zenrows and the LLM’s API keys to your environment variables.
  • Solution 2: Cross-check the API keys and ensure you’ve entered the correct keys.

Empty or incomplete data/tool response

  • Solution 1: Activate JS rendering to handle dynamic content and increase the success rate.
  • Solution 2: Increase the wait time using the Zenrows wait or wait_for parameter. The wait parameter introduces a general delay to allow the entire page to load, whereas wait_for targets a specific element, pausing execution until that element appears before scraping continues.
  • Solution 3: If you’ve used the css_extractor parameter to target specific elements, ensure you’ve entered the correct selectors.

Helpful Resources

Frequently Asked Questions (FAQ)

langchain-zenrows is compatible with all LLMs supported by LangChain. Check LangChain’s official chat models documentation for more information.
Yes, you can extract data from specific elements by explicitly specifying their selectors in your prompt.
Yes, you can include custom JavaScript via Zenrows’ js_instructions parameter. Check our JavaScript instructions guide for more.
Yes, Zenrows’ antibot bypass features are activated automatically when using Zenrows as the agent’s tool.
Yes. The JS rendering parameter is activated on demand while scraping a JavaScript-rendered site. This enables you to scrape dynamic pages with ease.
To extract data from specific elements, use Zenrows’ css_extractor parameter to specify the selectors of the elements containing the data you want to scrape.
Yes, you can prompt the LLM to take a half, full, or a specific element screenshot, and it will return your desired result using Zenrows’ screenshot parameter.
Zenrows offers enterprise-grade reliability, featuring built-in antibot bypass, premium proxies, JavaScript rendering, and more. Unlike basic scrapers, it can handle protected sites, geo-restricted content, and modern SPAs without getting blocked.