> For the complete documentation index, see [llms.txt](https://developers.oxylabs.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers.oxylabs.io/ai-workflows/pt-br/langchain.md).

# LangChain

Use o framework LangChain junto com a Web Scraper API da Oxylabs para extrair dados da web e alimentá-los em fluxos de trabalho de LLM — colete, processe, analise em um único pipeline.

A **LangChain** integração com a [**Oxylabs Web Scraper API**](https://oxylabs.io/products/scraper-api/web) permite coletar e processar dados da web por meio de um LLM (Large Language Model) no mesmo fluxo de trabalho.

## Visão geral

**LangChain** é uma estrutura para criar aplicativos que usam LLMs junto com ferramentas, APIs e dados da web. Ela é compatível com Python e JavaScript. Use-a com [**Oxylabs Web Scraper API** ](http://developers.oxylabs.io/scraper-apis/web-scraper-api?_gl=1*1ljhay3*_gcl_aw*R0NMLjE3NDYxODM0ODcuQ2owS0NRancydEhBQmhDaUFSSXNBTlp6RFdvSXlSNVg3blQtd0ZEakxHOUlvdUhyQmtoRTRCeUNwc054dFJVMmh0Z3dZTTR3Nm90SjVKOGFBbHhhRUFMd193Y0I.*_gcl_au*MjU4NDEzMTkwLjE3NDExNzU2MzI.)para:

* Raspar dados estruturados sem lidar com CAPTCHAs, bloqueios de IP ou renderização de JS
* Processar resultados com um LLM no mesmo pipeline
* Criar fluxos de trabalho ponta a ponta, da extração à saída com IA

## Primeiros passos

**Crie suas credenciais de usuário da API**: cadastre-se para uma avaliação gratuita ou compre o produto no [**painel da Oxylabs**](https://dashboard.oxylabs.io/en/registration) para criar suas credenciais de usuário da API (`USERNAME` e `PASSWORD`).

{% hint style="warning" %}
Se você precisar de mais de um usuário de API para sua conta, entre em contato com nosso [**suporte ao cliente**](mailto:support@oxylabs.io) ou envie uma mensagem para nosso suporte via chat ao vivo 24/7.
{% endhint %}

Neste guia, usaremos a linguagem de programação Python. Instale as bibliotecas necessárias usando pip:

```bash
pip install -qU langchain-oxylabs langchain-openai langgraph requests python-dotenv
```

## Configuração do ambiente

Crie um arquivo `.env` na pasta do seu projeto com suas credenciais de usuário da API da Oxylabs e da OpenAI:

```
OXYLABS_USERNAME=your-username
OXYLABS_PASSWORD=your-password
OPENAI_API_KEY=your-openai-key
```

Carregue essas variáveis de ambiente no seu script Python:

```python
import os
from dotenv import load_dotenv

load_dotenv()
```

## Métodos de integração

Há duas formas principais de integrar a Oxylabs Web Scraper API com o LangChain:

### Usando o pacote langchain-oxylabs

Para consultas de pesquisa no Google, use o [`langchain-oxylabs`](https://python.langchain.com/docs/integrations/tools/oxylabs/) pacote dedicado, que oferece uma integração pronta para uso:

```python
import os
from dotenv import load_dotenv
from langchain.chat_models import init_chat_model
from langgraph.prebuilt import create_react_agent
from langchain_oxylabs import OxylabsSearchAPIWrapper, OxylabsSearchRun

load_dotenv()

# Initialize your preferred LLM model
llm = init_chat_model(
    "gpt-4o-mini",
    model_provider="openai",
    api_key=os.getenv("OPENAI_API_KEY")
)

# Initialize the Google search tool
search = OxylabsSearchRun(
    wrapper=OxylabsSearchAPIWrapper(
        oxylabs_username=os.getenv("OXYLABS_USERNAME"),
        oxylabs_password=os.getenv("OXYLABS_PASSWORD")
    )
)

# Create an agent that uses the Google search tool
agent = create_react_agent(llm, [search])

Exemplo de uso
user_input = "When and why did the Maya civilization collapse?"
response = agent.invoke({"messages": user_input})
print(response["messages"][-1].content)
```

### Usando a Web Scraper API

Para acessar outros sites além da pesquisa do Google, você pode enviar a requisição diretamente para a Web Scraper API:

```python
import os
import requests
from dotenv import load_dotenv
from langchain_openai import OpenAI
from langchain_core.prompts import PromptTemplate

load_dotenv()

def scrape_website(url):
    """Scrape the website using Oxylabs Web Scraper API"""
    payload = {
        "source": "universal",
        "url": url,
        "parse": True
    }
    response = requests.post(
        "https://realtime.oxylabs.io/v1/queries",
        auth=(os.getenv("OXYLABS_USERNAME"), os.getenv("OXYLABS_PASSWORD")),
        json=payload
    )
    
    if response.status_code == 200:
        data = response.json()
        content = data["results"][0]["content"]
        return str(content)
    else:
        print(f"Failed to scrape website: {response.text}")
        return None

def process_content(content):
    """Process the scraped content using LangChain"""
    if not content:
        print("No content to process.")
        return None
        
    prompt = PromptTemplate.from_template(
        "Analyze the following website content and summarize key points: {content}"
    )
    chain = prompt | OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
    result = chain.invoke({"content": content})
    return result

def main(url):
    print("Scraping website...")
    scraped_content = scrape_website(url)
    if scraped_content:
        print("Processing scraped content with LangChain...")
        analysis = process_content(scraped_content)
        print("\nProcessed Analysis:\n", analysis)
    else:
        print("No content scraped.")

if __name__ == "__main__":
    url = "https://sandbox.oxylabs.io/products/1"
    main(url)
```

## Scrapers específicos por destino

A Oxylabs oferece [**scrapers especializados**](https://developers.oxylabs.io/api-targets/) para vários sites populares. Aqui estão alguns exemplos de fontes disponíveis:

| Site    | Parâmetro de origem | Parâmetros necessários       |
| ------- | ------------------- | ---------------------------- |
| Google  | `google_search`     | `query`                      |
| Amazon  | `amazon_search`     | `query`, `domain` (opcional) |
| Walmart | `walmart_search`    | `query`                      |
| Target  | `target_search`     | `query`                      |
| Kroger  | `kroger_search`     | `query`, `store_id`          |
| Staples | `staples_search`    | `query`                      |

Para usar um scraper específico, modifique o payload na função `scrape_website` função:

```python
# Example for Amazon search
payload = {
    "source": "amazon_search",
    "query": "smartphone",
    "domain": "com",
    "parse": True
}
```

## Configuração avançada

### Tratamento de conteúdo dinâmico

A Web Scraper API pode lidar com [**renderização de JavaScript**](broken://pages/7c8078bb3880926cbb8ab8760351a5c3a518192d) adicionando o parâmetro `render` :

```python
payload = {
    "source": "universal",
    "url": url,
    "parse": True,
    "render": "html"
}
```

### Definindo o tipo de user agent

Você pode especificar diferentes [**user agents**](broken://pages/956777d46de50bef618b443cd7adef7a8233fc8e) para simular diferentes dispositivos:

```python
payload = {
    "source": "universal",
    "url": url,
    "parse": True,
    "render": "html",
    "user_agent_type": "mobile"
}
```

### Usando parâmetros específicos do destino

Muitos [**scrapers específicos do destino**](https://developers.oxylabs.io/api-targets/) suportam parâmetros adicionais:

```python
# Example for Kroger with location parameters
payload = {
    "source": "kroger_search",
    "query": "organic milk",
    "store_id": "01100002",
    "fulfillment_type": "pickup"
}
```

## Tratamento de erros

Implemente um tratamento de erros adequado para aplicações em produção:

```python
try:
    response = requests.post(
        "https://realtime.oxylabs.io/v1/queries",
        auth=(os.getenv("OXYLABS_USERNAME"), os.getenv("OXYLABS_PASSWORD")),
        json=payload,
        timeout=60
    )
    response.raise_for_status()
    # Process response
except requests.exceptions.HTTPError as http_err:
    print(f"HTTP error occurred: {http_err}")
except requests.exceptions.ConnectionError as conn_err:
    print(f"Connection error occurred: {conn_err}")
except requests.exceptions.Timeout as timeout_err:
    print(f"Timeout error occurred: {timeout_err}")
except requests.exceptions.RequestException as req_err:
    print(f"An error occurred: {req_err}")
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://developers.oxylabs.io/ai-workflows/pt-br/langchain.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
