使用playwright异步自动化爬虫

由于时效问题,该文某些代码、技术可能已经过期,请注意!!!本文最后更新于:1 个月前

playwright

playwright代替selenium,异步爬虫

安装

1
2
pip install playwright
playwright install chromium # 安装浏览器 只安装chromium

使用

基本使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import asyncio
from playwright.async_api import async_playwright
from bs4 import BeautifulSoup # 导入 BeautifulSoup 库

async def fetch_data(url):

async with async_playwright() as p:
# 启动浏览器
browser = await p.chromium.launch(headless=False) # 可以设置 headless=True 以隐藏浏览器窗口
page = await browser.new_page()

# 访问页面
await page.goto(url, timeout=600000)
# 等待页面加载完成(可选,根据需要调整等待时间)
# await page.wait_for_load_state('networkidle')
await asyncio.sleep(3)

# 获取数据
content = await page.content()

# 打印获取到的内容(调试用)
# print(content)

# 关闭浏览器
await browser.close()

# 运行主异步函数
asyncio.run(fetch_data(url))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import asyncio
from playwright.async_api import async_playwright
from bs4 import BeautifulSoup # 导入 BeautifulSoup 库

usnews_dict = {}

async def fetch_data(page_number):
# 更新 URL 中的 page 参数
url = f"https://www.usnews.com/best-colleges/api/search?format=json&schoolType=national-universities&_sort=rank&_sortDirection=asc&_page={page_number}"

async with async_playwright() as p:
# 启动浏览器
browser = await p.chromium.launch(headless=False) # 可以设置 headless=True 以隐藏浏览器窗口
page = await browser.new_page()

# 访问页面
await page.goto(url)

# 获取 JSON 数据
json_data = await page.evaluate('''
(url) => {
return fetch(url)
.then(response => response.json())
.then(data => data)
.catch(error => { throw new Error(error) });
}
''', url)

# 处理 JSON 数据
items = json_data.get('data', {}).get('items', [])
for item in items:
name = item['institution']['displayName']
linkTxt = item['institution']['linkedDisplayName']
linkSoup = BeautifulSoup(linkTxt, 'html.parser')
link = 'https://www.usnews.com' + linkSoup.find('a').get('href')
usnews_dict[name] = link

# 打印数据
print(f"Page {page_number}:")
for name, link in usnews_dict.items():
print(f"{name}: {link}")

# 关闭浏览器
await browser.close()

async def main():
# 循环请求第 1 页到第 10 页的数据
for page_number in range(1, 11):
time.sleep(10)
await fetch_data(page_number)

# 运行主异步函数
asyncio.run(main())

with open('usnews-100.json', 'w') as json_file:
json.dump(usnews_dict, json_file, indent=2)


注意事项

  1. 异步爬虫,需要使用asyncio.run(main())来运行主函数
  2. 使用playwright时,需要使用async with async_playwright() as p:来启动浏览器
  3. 使用playwright时,需要使用await page.goto(url)来访问页面
  4. 使用playwright时,需要使用await page.evaluate(‘’’)来执行JavaScript代码
  5. 使用playwright时,需要使用await browser.close()来关闭浏览器
  6. 使用playwright时,需要使用await page.evaluate(‘’’)来获取页面数据

参考

playwright官方文档


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!