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