【问题标题】:How can I stop proxy when I reach to the site that I want?当我到达我想要的站点时如何停止代理?
【发布时间】:2022-12-13 00:13:49
【问题描述】:
try:
            with sync_playwright() as p:
                driver = p.firefox.launch(hedless=headless, proxy={
                    "server": 'fa****y.com:10000',
                    'username': 'iu***53cpeuytpna0cc7bd7',
                    "password": '**W7**Fm5',
                })
                context = driver.new_context()
                page = context.new_page()
                page.route("**/*",lambda route:route.abort()
                    if route.request.resource_type == "image"
                        or route.request.resource_type == "stylesheet"
                        or route.request.resource_type == "svg"
                        else route.continue_()
                )

                #page.set_default_timeout(100000)
               
                try:
                    url = 'https://accounts.google.com/v3/signin/identifier?dsh=S-536847501%3A1663770960047319&continue=https%3A%2F%2Fplay.google.com%2Fconsole%2Fsignup&followup=https%3A%2F%2Fplay.google.com%2Fconsole%2Fsignup&passive=1209600&service=androiddeveloper&flowName=GlifWebSignIn&flowEntry=ServiceLogin&ifkv=AQDHYWoj7hmeMm5YT3PrA0sojYcd3nnuAx2JkCLnedM0A9sCEUG9nrlRYD-grtVE1CcBagVSvXOG'

                    page.goto(url)
                except Exception:
                    print("      [+]  Time out Error ")   
                print(Fore.LIGHTBLUE_EX +"    [+]  Start >>> "  + self.gmail )
                page.fill('id=identifierId',self.gmail)
                btn = '#identifierNext > div > button'
                page.click(btn)
                page.wait_for_timeout(3000)
                inbpass = '#password > div.aCsJod.oJeWuf > div > div.Xb9hP > input'
                page.fill(inbpass,self.password)
                btnpass = '#passwordNext > div > button'
                page.click(btnpass)
                time.sleep(3)
                page.wait_for_timeout(3000)
                try:
                 page.locator("text=I understand").click(timeout=10000)
                 page.wait_for_timeout(1500)
                 sleep(1)
                except:
                    pass

                try:

                  page.locator("div[role=\"link\"]:has-text(\"Confirm your recovery email\")").click()
                  page.wait_for_timeout(3000)
                  page.locator("[aria-label=\"Enter recovery email address\"]").fill(self.recvery)
                  page.wait_for_timeout(3000)
                  time.sleep(3)
                  # Click button:has-text("Next")
                  page.locator("button:has-text(\"Next\")").click()
                  time.sleep(10)
                
                except:
                    pass
            


                page.locator("text=YourselfChoose if your account is for personal  >> button").click()
                time.sleep(3)
                page.wait_for_timeout(1500)
**IN THIS LINE:
(In this line : I want to make proxy stop because it's not unlimited and I am paying for GB and I want to use only local internet from this line to end is there any help ?)**

我想在点击时停止使用代理 一:因为代理使用率很高,下一步我不再需要代理了。 二:我想让它更快,因为代理到达页面时速度很慢。 请帮我写代码或任何我不知道如何解决的问题

【问题讨论】:

  • 不幸的是,你不能。
  • 我无法在没有代理的情况下打开另一个页面并关闭第一个页面并继续工作

标签: playwright playwright-python


【解决方案1】:

没有直接的方法可以实现这一点,但是您可以使用一些变通方法来使用 routing 获得相同的结果。基本上,在您不再需要代理之后,您将 playwright 发出的所有请求路由到一个处理程序,该处理程序随后绕过代理并将响应转发回 playwright。然而,这种方法只适用于 playwright 的异步版本(否则从路由处理程序执行阻塞调用也会阻塞进一步的网络流量)。考虑以下代码:

import aiohttp, asyncio
import playwright


def no_proxy_route(session):
    """
    Bypasses any proxy and routes request directly. It's a factory function to store value of session so we don't need
    to create a new one every time.
    """

    async def route_handler(route: playwright.async_api.Route):

        request = route.request

        try:
            body, status, headers = await fetch(session, request)

        # Abort route in case of error
        except (aiohttp.ClientConnectionError, asyncio.TimeoutError):
            await route.abort()
            return

        # If no error, fulfill route with the given body and headers
        await route.fulfill(status=status, headers=headers, body=body)

    return route_handler


async def fetch(session, request, timeout=5000):
    """
    Fetch the given request using aiohttp
    """

    assert timeout > 0, "timeout must be positive"
    try:
        async with session.request(request.method, request.url, headers=request.headers,
                                   data=request.post_data, timeout=timeout) as response:
            body = await response.read()
            status = response.status
            headers = response.headers

    except Exception:
        raise

    else:
        return body, status, headers

现在,如果你想有条件地为某些请求禁用代理,你只需创建一个匹配模式并将所有这些请求路由到no_proxy_route。下面给出一个例子:

### All your previous code
# .
# .
# .
# .
### which required a proxy

# Create an aiohttp session with a dummy cookie jar. This is because we will be passing the cookies
# explicitly and don't want previous requests/responses cookies to persist and interfere
session = aiohttp.ClientSession(cookie_jar=aiohttp.DummyCookieJar())

# Create a route to our handler with an appropriate pattern. The below pattern will route ALL subsequent requests to 
# handler. Remember that you can use regex for patterns as well.
await page.route('**/*', no_proxy_route(session))

完成此操作后,所有与您创建路由的模式匹配的请求都不会使用代理,即使上下文有一组也是如此。但同样,这仅在您使用剧作家的异步 API 时才有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 2014-08-18
    • 1970-01-01
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多