爬虫案例—抓取找歌词网站的按歌词找歌名数据

简介: 爬虫案例—抓取找歌词网站的按歌词找歌名数据

爬虫案例—抓取找歌词网站的按歌词找歌名数据
找个词网址: https://www.91ge.cn/lxyyplay/find/

目标:抓取页面里的所有要查的歌词及歌名等信息,并存为txt文件

一共46页数据

网站截图如下:
image.png

抓取完整歌词数据,如下图:
image.png

源码如下:

import asyncio
import time
import aiohttp
from aiohttp import TCPConnector  # 处理ssl验证报错
from lxml import etree

headers = {
   
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
}


# 返回每首歌的href函数
async def get_song_url(page_url):
    async with aiohttp.ClientSession(headers=headers, connector=TCPConnector(ssl=False)) as session:
        async with session.get(page_url) as res:
            html = await res.text()
            tree = etree.HTML(html)
            url_lst = tree.xpath('//div[@class="des"]/a/@href')
    return url_lst


# 获取每首歌的详细信息
async def get_song_word(song_url):
    async with aiohttp.ClientSession(headers=headers, connector=TCPConnector(ssl=False)) as session:
        async with session.get(song_url) as res:
            html = await res.text()
            tree = etree.HTML(html)
            if tree is not None:
                song_question = tree.xpath('//div[@class="logbox"]')
                if song_question:
                    song_q = song_question[0].xpath('./h1/text()')[0]
                else:
                    pass
                div_word = tree.xpath('//div[@class="logcon"]')
                if div_word:
                    where_song = div_word[0].xpath('./h2[1]/text()')[0]
                    question_song = div_word[0].xpath('./p[1]/text()')[0]
                    answer_song = div_word[0].xpath('./p[2]/text()')[0]
                    song_words = div_word[0].xpath('./p[position()>2]//text()')
                    # song_name = div_word.xpath('./h2[2]/text()')[0].strip('\r\n\t')
                    song_words = ''.join(song_words[:-1]).strip('\r\n\t')

                    with open(f'songs/{song_q}.txt', 'a') as f:
                        f.write(where_song + '\n' + question_song + '\n' + answer_song + '\n\n' + song_words)
            else:
                pass


if __name__ == '__main__':
    t1 = time.time()
    loop = asyncio.get_event_loop()
    for n in range(1, 47):
        song_url = f'https://www.91ge.cn/lxyyplay/find/list_16_{n}.html'
        urls = loop.run_until_complete(get_song_url(song_url))
        tasks = [get_song_word(url) for url in urls]
        loop.run_until_complete(asyncio.gather(*tasks))

    print(f'耗时:{time.time() - t1:.2f}秒')

运行结果如下图:

image.png

利用协程抓取数据,效率很高。

相关文章
|
3月前
|
数据采集 存储 前端开发
动态渲染爬虫:Selenium抓取京东关键字搜索结果
动态渲染爬虫:Selenium抓取京东关键字搜索结果
|
3月前
|
数据采集 存储 前端开发
Java爬虫性能优化:多线程抓取JSP动态数据实践
Java爬虫性能优化:多线程抓取JSP动态数据实践
|
1月前
|
数据采集 监控 数据库
Python异步编程实战:爬虫案例
🌟 蒋星熠Jaxonic,代码为舟的星际旅人。从回调地狱到async/await协程天堂,亲历Python异步编程演进。分享高性能爬虫、数据库异步操作、限流监控等实战经验,助你驾驭并发,在二进制星河中谱写极客诗篇。
Python异步编程实战:爬虫案例
|
3月前
|
数据采集 存储 JSON
地区电影市场分析:用Python爬虫抓取猫眼/灯塔专业版各地区票房
地区电影市场分析:用Python爬虫抓取猫眼/灯塔专业版各地区票房
|
3月前
|
数据采集 存储 XML
Python爬虫XPath实战:电商商品ID的精准抓取策略
Python爬虫XPath实战:电商商品ID的精准抓取策略
|
4月前
|
数据采集 存储 NoSQL
Python爬虫案例:Scrapy+XPath解析当当网网页结构
Python爬虫案例:Scrapy+XPath解析当当网网页结构
|
5月前
|
数据采集 Web App开发 JavaScript
Python爬虫解析动态网页:从渲染到数据提取
Python爬虫解析动态网页:从渲染到数据提取
|
6月前
|
数据采集 存储 前端开发
Python爬虫自动化:批量抓取网页中的A链接
Python爬虫自动化:批量抓取网页中的A链接
|
7月前
|
数据采集 测试技术 C++
无headers爬虫 vs 带headers爬虫:Python性能对比
无headers爬虫 vs 带headers爬虫:Python性能对比
|
数据采集 存储 JSON
Python网络爬虫:Scrapy框架的实战应用与技巧分享
【10月更文挑战第27天】本文介绍了Python网络爬虫Scrapy框架的实战应用与技巧。首先讲解了如何创建Scrapy项目、定义爬虫、处理JSON响应、设置User-Agent和代理,以及存储爬取的数据。通过具体示例,帮助读者掌握Scrapy的核心功能和使用方法,提升数据采集效率。
501 6