Skip to content

Commit

Permalink
community[proxy]: Enhancement/add proxy support playwrighturlloader 1…
Browse files Browse the repository at this point in the history
…6751 (#16822)

- **Description:** Enhancement/add proxy support playwrighturlloader
16751
- **Issue:** [Enhancement: Add Proxy Support to PlaywrightURLLoader
Class](#16751)
  - **Dependencies:** 
  - **Twitter handle:** @ootR77013489

---------

Co-authored-by: root <root@ip-172-31-46-160.ap-southeast-1.compute.internal>
Co-authored-by: Bagatur <baskaryan@gmail.com>
  • Loading branch information
3 people committed Feb 13, 2024
1 parent e3b775e commit c454dc3
Showing 1 changed file with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""
import logging
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, List, Optional
from typing import TYPE_CHECKING, Dict, List, Optional

from langchain_core.documents import Document

Expand Down Expand Up @@ -111,6 +111,22 @@ class PlaywrightURLLoader(BaseLoader):
urls (List[str]): List of URLs to load.
continue_on_failure (bool): If True, continue loading other URLs on failure.
headless (bool): If True, the browser will run in headless mode.
proxy (Optional[Dict[str, str]]): If set, the browser will access URLs
through the specified proxy.
Example:
.. code-block:: python
from langchain_community.document_loaders import PlaywrightURLLoader
urls = ["https://api.ipify.org/?format=json",]
proxy={
"server": "https://xx.xx.xx:15818", # https://<host>:<port>
"username": "username",
"password": "password"
}
loader = PlaywrightURLLoader(urls, proxy=proxy)
data = loader.load()
"""

def __init__(
Expand All @@ -120,6 +136,7 @@ def __init__(
headless: bool = True,
remove_selectors: Optional[List[str]] = None,
evaluator: Optional[PlaywrightEvaluator] = None,
proxy: Optional[Dict[str, str]] = None,
):
"""Load a list of URLs using Playwright."""
try:
Expand All @@ -133,6 +150,7 @@ def __init__(
self.urls = urls
self.continue_on_failure = continue_on_failure
self.headless = headless
self.proxy = proxy

if remove_selectors and evaluator:
raise ValueError(
Expand All @@ -153,7 +171,7 @@ def load(self) -> List[Document]:
docs: List[Document] = list()

with sync_playwright() as p:
browser = p.chromium.launch(headless=self.headless)
browser = p.chromium.launch(headless=self.headless, proxy=self.proxy)
for url in self.urls:
try:
page = browser.new_page()
Expand Down Expand Up @@ -186,7 +204,7 @@ async def aload(self) -> List[Document]:
docs: List[Document] = list()

async with async_playwright() as p:
browser = await p.chromium.launch(headless=self.headless)
browser = await p.chromium.launch(headless=self.headless, proxy=self.proxy)
for url in self.urls:
try:
page = await browser.new_page()
Expand Down

0 comments on commit c454dc3

Please sign in to comment.