python协程爬小说

协程(coroutine)是一种比线程更轻量级的并发执行单位,他们允许函数在执行过程中阻塞暂停,,并在需要的时候恢复执行,,从而实现协作式多任务处理,,跟传统的线程不同,协程不需要依赖操作系统的调度,,因为始终都在一个线程里,,里面有一个 event_loop 去监测哪一部分需要阻塞,,一但阻塞,就切换到不被阻塞的代码执行,,

通过 await 进行挂起和恢复,,,异步IO


xpath获取页面内容

from lxml import etree
etree.xpath(...)

开启协程:

asyncio.run(方法名)

# 或者
 # 事件轮询 监测函数
  event_loop = asyncio.get_event_loop()
  event_loop.run_until_complete(main())

协程函数,都有一个关键字async

async def mian():
	# 创建任务  asyncio.create_task(...)
	 
	# 等待所有任务执行完成. ,,, done:已经完成的任务,,是一个set对象,无序的,, 通过t.result()返回协程任务的返回值,,如果想要是有序的,使用gather函数
	done,pending = await asyncio.wait(tasks)

	# gather会直接返回有序的数组,一般return_exception设置为True,遇到错误会返回在数组中,,如果设置为False,遇到错误会抛错,代码无法执行
	result = await asyncio.gather(*task,return_exception=True)

协程发送请求和存文件:

import aiohttp
import aiofiles

# aiohttp发送请求 和 requests类似
async with aiohttp.ClientSession() as session:
              async with session.get(href) as resp:
              # aiohttp设置编码
                  page_source = await resp.text(encoding="gbk")

async with aiofiles.open(f"./hehe/{title}.txt",mode="w",encoding="utf-8") as f:
                        await f.write(content)

如果文件写入失败,重试请求:
在这里插入图片描述
代码:

import asyncio

import aiofiles
import aiohttp
import requests
from lxml import etree

def get_href_list():
    resp = requests.get("https://www.biquge635.com/mingchaonaxieshier/")
    resp.encoding = "gbk"
    href_list = etree.HTML(resp.text).xpath("//div[@class='section-box']/ul/li/a/@href")
    print(href_list)
    return href_list


async def download_one(href):
    print("开始下载%s"%href)
    while 1:
        try:
            # aio 异步io
            async with aiohttp.ClientSession() as session:
                async with session.get(href) as resp:
                    page_source = await resp.text(encoding="gbk")
                    # print(page_source)
                    tree = etree.HTML(page_source)
                    title = tree.xpath("//h1[@class='title']/text()")[0].strip()
                    content = tree.xpath("//div[@id='content']/text()")

                    content = "\n".join(content).replace("\u3000", "")
                    async with aiofiles.open(f"./hehe/{title}.txt", mode="w", encoding="utf-8") as f:
                        await f.write(content)
                        break

        except:
            print("重试%s"%href)

    print("下载结束%s" % href)
async def download(href_list):
    tasks = []
    for href in href_list:
        tasks.append(asyncio.create_task(download_one(href)))
    await asyncio.wait(tasks)

def main():
    href_list = get_href_list()

    # 启动协程
    asyncio.run(download(href_list))


if __name__ == '__main__':
    main()

相关推荐

  1. Python小说

    2024-06-16 06:44:04       11 阅读
  2. python

    2024-06-16 06:44:04       41 阅读
  3. python爬虫之

    2024-06-16 06:44:04       28 阅读
  4. python理解笔记,

    2024-06-16 06:44:04       20 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-16 06:44:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-16 06:44:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-16 06:44:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-16 06:44:04       20 阅读

热门阅读

  1. 【学习笔记】MySQL(Ⅲ)

    2024-06-16 06:44:04       8 阅读
  2. 【手机】米10替换基带

    2024-06-16 06:44:04       7 阅读
  3. Cheat Engine.exe修改植物大战僵尸阳光与冷却

    2024-06-16 06:44:04       9 阅读
  4. 模拟题1(考虑周全以及情况较多)

    2024-06-16 06:44:04       6 阅读
  5. 深入理解Python闭包:概念、应用与示例

    2024-06-16 06:44:04       10 阅读
  6. 目录深度探索

    2024-06-16 06:44:04       7 阅读
  7. Leetcode.2601 质数减法运算

    2024-06-16 06:44:04       8 阅读
  8. Web前端图形显示:深入探索与实用指南

    2024-06-16 06:44:04       9 阅读
  9. seata源码分析(04)_TM开启全局事务

    2024-06-16 06:44:04       5 阅读
  10. 738. 单调递增的数字

    2024-06-16 06:44:04       9 阅读
  11. QStateMachine 笔记

    2024-06-16 06:44:04       5 阅读
  12. 笔记-python 中BeautifulSoup入门

    2024-06-16 06:44:04       10 阅读