asyncio 异步爬虫(转)

本文分享了一篇关于使用asyncio进行异步爬虫的代码实例,旨在方便个人日后查阅。通过asyncio库,可以提高爬虫的效率,实现并发请求,提升数据抓取速度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载过来的代码,方便自己查看,勿怪

# 异步方式爬取当当畅销书的图书信息
import aiohttp
import asyncio
import time
import pandas as pd
from bs4 import BeautifulSoup

table = []

async def fetch(session, url):
    async with session.get(url) as response:
        return await response.text(encoding='gb18030')

async def parser(html):
    soup = BeautifulSoup(html,'lxml')
    book_list = soup.find('ul',class_="bang_list clearfix bang_list_mode")('li')
    for book in book_list:
        info = book.find_all('div')
        rank = info[0].text[0:-1]
        name = info[2].text
        comments = info[3].text.split('条')[0]
        author = info[4].text
        date_and_publisher = info[5].text.split()
        publisher = date_and_publisher[1] if len(date_and_publisher) >= 2 else ''

        table.append([rank,name,comments,author,publisher])

async def download(url):
    async with aiohttp.ClientSession() as session:
        html = await fetch(session, url)
        await parser(html)

urls = ['http://bang.dangdang.com/books/bestsellers/01.00.00.00.00.00-recent7-0-0-1-%d'%i for i in range(1,26)]
print('#' * 50)
t1 = time.time()

# 利用asyncio模块进行异步IO处理
loop = asyncio.get_event_loop()
tasks = [asyncio.ensure_future(download(url)) for url in urls]
tasks = asyncio.gather(*tasks)
loop.run_until_complete(tasks)

df = pd.DataFrame(table, columns=['rank','name','comments','author','publisher'])

df.to_excel('E:\\dangdang.xlsx', index=False)
t2 = time.time()
print('使用aiohttp,总共耗时:%s' % (t2 - t1))
print('#' * 50)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值