如何使用剧作家捕获特定的重定向?

发布于 2025-02-10 15:25:39 字数 1713 浏览 1 评论 0 原文

当Google地图达到某个级别时,确认了一个地点搜索,它将其重定向到特定的Google Place URL,否则它将返回地图搜索结果页面。

Google Map搜索“ Manarama”是

https://www.google.com/maps/search/manarama/@23.7505522,90.3616303,15Z/Data=!4m2!2m1!6e6

,将其重新送给Google Place url

https://www.google.com/maps/place/Manarama,+29+Rd+No.+14A,+Dhaka+1209/@23.7505522,90.3616303 ,15z/data =!4m5!3m4!1S0X3755BF4DFC183459:0xB9127B8C3072C249!8M2!3D23.750523

4D90.3703851 =“ https://www.google.com/maps/search/mana/@24.211316,89.340686,8z/data=!3M1!4B1“ rel =“ nofollow noreferrer” /search/mana/@24.211316,89.340686,8z/data=!3M1!4B1

import asyncio
from playwright.async_api import async_playwright

async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False)
        page = await browser.new_page()
        await page.goto("https://www.google.com/maps/search/manarama/@23.7505522,90.3616303,15z/data=!4m2!2m1!6e6", wait_until="networkidle")
        print(page.url) 
        await page.close()
        await browser.close()

asyncio.run(main())

有时它会返回重定向的URL,但大多数情况下,它不是。怎么知道URL肯定会重定向到一个位置URL?以下stackoverflow帖子具有相似之处,但不能使它适合我的情况

如何使用playwright使用WebApp捕获重定向

when Google Map is to some level confirmed about a place search it redirects to the specific Google place url otherwise it returns a map search result page.

Google Map search for "manarama" is

https://www.google.com/maps/search/manarama/@23.7505522,90.3616303,15z/data=!4m2!2m1!6e6

which redirects to a Google Place URL

https://www.google.com/maps/place/Manarama,+29+Rd+No.+14A,+Dhaka+1209/@23.7505522,90.3616303,15z/data=!4m5!3m4!1s0x3755bf4dfc183459:0xb9127b8c3072c249!8m2!3d23.750523!4d90.3703851

Google Map search result page looks like the following link below when it is not confirmed about the specific place

https://www.google.com/maps/search/Mana/@24.211316,89.340686,8z/data=!3m1!4b1

import asyncio
from playwright.async_api import async_playwright

async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False)
        page = await browser.new_page()
        await page.goto("https://www.google.com/maps/search/manarama/@23.7505522,90.3616303,15z/data=!4m2!2m1!6e6", wait_until="networkidle")
        print(page.url) 
        await page.close()
        await browser.close()

asyncio.run(main())

Sometimes it returns the redirected URL, but most of the time, it doesn't. How to know the URL got redirected to a place URL for sure? the following StackOverflow post has similarities but couldn't make it work for my case

How to catch the redirect with a webapp using playwright

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌辣 2025-02-17 15:25:39

您可以使用 nofollow noreferrer“> expect_navigation

在您提到的有关与该功能匹配的URL的评论中。几乎所有这样的剧作家功能都接受 regex 模式。因此,如有疑问,只需使用正则。请参阅下面的代码:

import asyncio
from playwright.async_api import async_playwright, TimeoutError
import re

pattern = re.compile(r"http.*://.+?/place.+")


async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False)
        page = await browser.new_page()
        try:
            async with page.expect_navigation(url=pattern, timeout=7000) as resp:
                await page.goto(
                    "https://www.google.com/maps/search/manarama/@23.7505522,90.3616303,15z/data=!4m2!2m1!6e6",
                    wait_until='networkidle')
        except TimeoutError:
            print('place not found')
        else:
            print('navigated to place')

        print(page.url)
        await page.close()
        await browser.close()

asyncio.run(main())

为了检查页面是否导航,只需在中包装函数。 )到期望_navigation 。然后,如果提出了超时错误,您知道没有任何与我们模式相匹配的URL更改。

You can use expect_navigation.

In the comments you mentioned about what url to match for with the function. Almost all such playwright functions accept regex patterns. So when in doubt, just use regex. See the code below:

import asyncio
from playwright.async_api import async_playwright, TimeoutError
import re

pattern = re.compile(r"http.*://.+?/place.+")


async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False)
        page = await browser.new_page()
        try:
            async with page.expect_navigation(url=pattern, timeout=7000) as resp:
                await page.goto(
                    "https://www.google.com/maps/search/manarama/@23.7505522,90.3616303,15z/data=!4m2!2m1!6e6",
                    wait_until='networkidle')
        except TimeoutError:
            print('place not found')
        else:
            print('navigated to place')

        print(page.url)
        await page.close()
        await browser.close()

asyncio.run(main())

In order to check whether the page navigated or not, just wrap the function inside a try..except block and pass a suitable timeout argument (in ms) to expect_navigation. Then if a Timeout error was raised, you know that there wasn't any url change which matched our pattern.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文