asyncio官网

https://docs.python.org/zh-cn/3/library/asyncio-task.html

下面为伪代码:

import aiohttp

import asyncio

from bs4 import BeautifulSoup import pandas as pd # 将数据存入 li=[]或数据库 #获取页面 li=[]
async def fetch(url,session): async with session.get(url) as response: return await response.text # 解析网页 async def parse(html): soup = BeautifulSoup(html,'lxml') # 获取网页中的畅销书
book_list=soup.find('ul',class_='book_list')('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中
li.append([rank, name, comments, author, publisher]) # 处理页面 async def download(url): async with aiohttp.ClientSession as session: # 获取页面
html = await fetch(session, url) # 解析页面
await
parse(html) # 全部网页urls urls=[url1,url2,url3] # 利用asycio 模块进行一步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) # 将table转化为pandas中的DataFrame并保存为CSV格式的文件
df = pd.DataFrame(li, columns=['rank','name','comments','author','publisher']) df.to_csv('E://douban/dangdang.csv',index=False)

asyncio和aiohttp的更多相关文章

  1. 异步:asyncio和aiohttp的一些应用(1)

    1. asyncio 1.1asyncio/await 用法 async/await 是 python3.5中新加入的特性, 将异步从原来的yield 写法中解放出来,变得更加直观. 在3.5之前,如 ...

  2. Python有了asyncio和aiohttp在爬虫这类型IO任务中多线程/多进程还有存在的必要吗?

    最近正在学习Python中的异步编程,看了一些博客后做了一些小测验:对比asyncio+aiohttp的爬虫和asyncio+aiohttp+concurrent.futures(线程池/进程池)在效 ...

  3. 异步:asyncio和aiohttp的一些应用(2)

    转自:原文链接:http://www.cnblogs.com/ssyfj/p/9222342.html 1.aiohttp的简单使用(配合asyncio模块) import asyncio,aioht ...

  4. asyncio 实现 aiohttp

    #asyncio 没有提供http协议的接口 aiohttp import asyncio import socket from urllib.parse import urlparse async ...

  5. python链家网高并发异步爬虫asyncio+aiohttp+aiomysql异步存入数据

    python链家网二手房异步IO爬虫,使用asyncio.aiohttp和aiomysql 很多小伙伴初学python时都会学习到爬虫,刚入门时会使用requests.urllib这些同步的库进行单线 ...

  6. aiohttp/asyncio 多次请求

    #!/usr/bin/env python # -*- coding: utf-8 -*- __author__ = "Daniel Altiparmak (sixfinger78@gmai ...

  7. aiohttp/asyncio测试代理是否可用

    #!/usr/bin/env python # encoding: utf-8 from proxyPool.db import RedisClient import asyncio import a ...

  8. Python使用asyncio+aiohttp异步爬取猫眼电影专业版

    asyncio是从pytohn3.4开始添加到标准库中的一个强大的异步并发库,可以很好地解决python中高并发的问题,入门学习可以参考官方文档 并发访问能极大的提高爬虫的性能,但是requests访 ...

  9. 异步协程asyncio+aiohttp

    aiohttp中文文档 1. 前言 在执行一些 IO 密集型任务的时候,程序常常会因为等待 IO 而阻塞.比如在网络爬虫中,如果我们使用 requests 库来进行请求的话,如果网站响应速度过慢,程序 ...

随机推荐

  1. 【LeetCode】645. Set Mismatch 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Hash方法 直接计算 日期 题目地址: https ...

  2. MCU变量加载过程

    前言 在开发mcu代码的时候经常会有些疑惑,变量是怎么在编译之后进入单片机的ram区的呢,特别是在使用keil开发的时候.后来在接触gcc编译器和自研的mcu后,终于明白了这个问题.实际上变量编译后被 ...

  3. P4081 [USACO17DEC]Standing Out from the Herd P

    知识点: 广义 SAM 原题面 Luogu 「扯」 随便「口胡」一下居然「过」了. 比较考验「代码能力」,第一次感觉「大模拟」没有白写((( 还有这个「符号」实在是太「上头」了. 前置知识 在线构造广 ...

  4. uniapp动态修改导航栏

    1.修改导航栏buttons 如图动态修改角标 <template> <view> </view> </template> <script> ...

  5. 【MySQL作业】DDL 和 DML——美和易思使用 DML 删除表数据应用习题

    点击打开所使用到的数据库>>> 删除客户"刘一鸣". 执行 SQL 代码"delete from customer where cName=' 刘一鸣 ...

  6. HTTP 状态码整理

  7. CentOS7中安装pip的方法

    1.安装epel-release [root@localhost ~]# yum -y install epel-release 2.安装python-pip [root@localhost ~]# ...

  8. nvm切换node版本出现乱码 exit status 1:

    nvm切换nodejs版本出现exit status 1:乱码 跟着网上的教程一步一步做,还是出现问题.浪费一下午的时间 最后发现却因为我没用CMD管理员权限运行 扑街 解决方法: 用管理员身份运行就 ...

  9. Git 的配置 config

    Git 的配置 config Git 的配置 config config 文件简述 config 文件位置 信息查询 修改 config 文件 编辑配置文件 增加指定配置项 删除指定配置项 自助餐   ...

  10. SCALA-基础知识学习(一)

    概述 本人开始学习scala的时候,是在使用和开发spark程序的时候,在此为了整理.记录和分享scala的基础知识,我写这篇关于scala的基础知识,希望与广大读者共同学习沟通进步.如果有些代码比较 ...