首页 > 其他分享 >异步爬虫demo2

异步爬虫demo2

时间:2023-06-04 15:24:09浏览次数:39  
标签:异步 url self 爬虫 demo2 headers session async main

import re
import aiohttp
import asyncio


class Asyn:
    def __init__(self):
        self.__headers = {
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.48',
        }

    async def fetch(self, session, url):
        print("Sending request:", url)
        async with session.get(url, headers=self.__headers) as response:
            content = await response.text()
            poetry_infos = re.findall(r'textarea style=".*>(.*?)——(.*?)https://so.gushiwen.cn/shiwenv_.*.aspx</textarea>', content)
            for item in poetry_infos:
                print(item)
                # Uncomment the lines below to write the data to a file
                with open('古诗内容111.txt', 'a', encoding='utf-8') as f:
                    f.write(f'{item}\n')

    async def main(self):
        async with aiohttp.ClientSession() as session:
            url = "https://so.gushiwen.org/gushi/tangshi.aspx"
            html = await session.get(url, headers=self.__headers)
            lsc = re.findall(r'<span><a href="/(.*)" target="_blank">.*</a>.*</span>', await html.text())
            tasks = [self.fetch(session, f"https://so.gushiwen.org/{path}") for path in lsc]
            await asyncio.gather(*tasks)

# Create an instance and run the main method
asyn = Asyn()
# asyncio.run(asyn.main())  # this error so use
loop = asyncio.get_event_loop()
loop.run_until_complete(asyn.main())

标签:异步,url,self,爬虫,demo2,headers,session,async,main
From: https://www.cnblogs.com/code3/p/17455710.html

相关文章

  • 网络爬虫--爬取泉州,福州,厦门历史天气和近七日天气--数据可视化
     一:选题背景:通过爬取各个城市的历史天气数据及近日数据,来进行对天气的数据分析与可视化。从而帮助我们的生活更加便利。所爬取的网站为2345天气王https://tianqi.2345.com/wea_forty/71924.htm这里我们进入网站后右键检查,点击网络这一栏,并刷新页面能够看到有两个文件,而我们所......
  • Servlet3.0新功能: 异步处理
    2EE6和Glassfish3V正式发布了,J2EE6正式发布了Servlet3.0,为了能更好的对WEB2.0提供支持,3.0添加了异步处理的机制.HTTP1.1相对于HTTP1.0的影响.HTTP1.1最大的一个改变就是提供了长连接,这样HTTP不再是一次请求,一次连接的协议了,只要HTTP的connection不关闭,一次HTTP连接......
  • Async异步注解
    @RestController@Slf4jpublicclassThread04{@AutowiredprivateOrderManageorderManage;@GetMapping("addOrder")publicStringaddOrder(){log.info("1");//异步处理orderManage.asyncLog();......
  • 02 python爬虫-bs4
    目录步骤代码结果new.csv图片步骤爬取主页面中的文章详情的url和图片地址下载图片并请求加文章详情中的页面内容爬取文章详情中的标题、作者、发布时间代码importrequestsimportcsvfrombs4importBeautifulSoup#请求的一级页面的地址q_url='http://xinfadi.com.cn/new......
  • 面试题:如何理解 JS的异步?
    JS是一门单线程的语言,这是因为它运行在浏览器的渲染主线程中,而渲染主线程只有一个而渲染主线程承担着诸多的工作,渲染页面、执行JS都在其中运行。如果使用同步的方式,就极有可能导致主线程产生阻塞,从而导致消息队列中的很多其他任务无法得到执行。这样一来,一方面会导致繁忙的主线......
  • Java中的爬虫
    爬虫pattern:表示正则表达式Matcher:文本匹配器,作用按照正则表达式的规则去读取字符串,从头开始读取步骤:获取正则表达式的对象Patternp=Pattern.compile("正则表达式");获取文本匹配器的对象Stringstr="文本";Matcherm=p.matcher(str);p:规则,str:大串,m;文本匹......
  • 字符设备驱动-7.异步通知
    1.异步通知概述前面讲到APP读取按键方式里面包含4种方式:1.查询方式,2.休眠唤醒,3,poll机制的休眠唤醒,4.异步通知什么是异步通知?你去买奶茶:◼你在旁边等着,眼睛盯着店员,生怕别人插队,他一做好你就知道:你是主动等待他做好,这叫“同步”。◼你付钱后就去玩手机了,店员做好后他会打电......
  • 基于Selenium库模拟用户行为的QQ空间爬虫
    QQ空间爬虫代码链接:https://github.com/hhr346/QQ_zone_crawl该程序的主要实现过程是进入某位好友的空间中,并自动爬取说说的文本内容并存储到txt文件中。方法是使用Chrome浏览器的自动执行功能,来实现翻页,并对html的源代码进行文本提取,通过正则表达式来实现某一块内容的提取。将......
  • 使用Python爬虫爬取网络美女图片
    准备工作安装python3.6略安装requests库(用于请求静态页面)pipinstallrequests-ihttps://mirrors.ustc.edu.cn/pypi/web/simple安装lxml库(用于解析html文件)pipinstalllxml-ihttps://mirrors.ustc.edu.cn/pypi/web/simple安装与配置selenium(用于请求动态页面)pipinstall......
  • # yyds干货盘点 # #经验分享# #网络爬虫# #数据分析# #Python# #每日打卡# #进阶学习#
    大家好,我是皮皮。一、前言前几天在Python群【洋洋】问了一个Python基础的问题,这里拿出来给大家分享下。二、实现过程这里【kim】给出了代码,如下所示:的确满足了粉丝的需求。很多人应该和我一样,想到的是zip吧。zip完全可以,可是他说要for,所以上面演示的是for循环。那么如果通过zip函数......