2

我编写了一个脚本,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))
4

2 回答 2

2

我会这样做:

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))
于 2018-12-04T21:24:30.627 回答
1

试试看:

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)

    names = await page.querySelectorAllEval('div.v-card .business-name span',
                                 '(elements => elements.map(e => e.textContent))')
    phones = await page.querySelectorAllEval('div.v-card .phones', 
                                 '(elements => elements.map(e => e.textContent))')
    result = {name: phones[idx] for (idx, name) in enumerate(names)}
    print(result)
    await wb.close()

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

并阅读文档:querySelectorAllEval

于 2018-11-26T12:41:01.200 回答