gpt4 book ai didi

python - 使用 pyppeteer 创建 for 循环的理想方法

转载 作者:行者123 更新时间:2023-12-01 08:41:17 25 4
gpt4 key购买 nike

我用 python 结合 pyppeteer 编写了一个脚本,用于从网页上抓取不同咖啡馆的名称及其电话号码。虽然我下面尝试的方法达到了目的,但脚本看起来确实很困惑。使用 pyppeteer 库创建 for 循环的理想方法是什么?

到目前为止我已经写过:

import asyncio
from pyppeteer import launch

url = "https://www.yellowpages.com/search?search_terms=coffee&geo_location_terms=Los%20Angeles%2C%20CA"

async def get_names(link):
wb = await launch(headless=True)
page = await wb.newPage()
await page.goto(link)

containers = await page.querySelectorAll('div.v-card')
for container in containers:
name = await container.querySelector('.business-name span')
phone = await container.querySelector('.phones')
post = await page.evaluate('(element) => element.textContent', name)
postAno = await page.evaluate('(element) => element.textContent', phone)
print(f'{post}--{postAno}')

await wb.close()

asyncio.get_event_loop().run_until_complete(get_names(url))

最佳答案

我会这样做:

import asyncio
from pyppeteer import launch

url = "https://www.yellowpages.com/search?search_terms=coffee&geo_location_terms=Los%20Angeles%2C%20CA"

async def get_names(link):
wb = await launch()
page = await wb.newPage()
await page.goto(link)

containers = await page.querySelectorAll('div.v-card')
for container in containers:
name = await container.querySelectorEval('.business-name span','e => e.innerText')
phone = await container.querySelectorEval('.phones','e => e.innerText')
print(name,phone)

asyncio.get_event_loop().run_until_complete(get_names(url))

关于python - 使用 pyppeteer 创建 for 循环的理想方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53481018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com