爬虫常用模板

这里记录的是我学习爬虫自己用到的一些东西,方便以后打开直接使用。我会不定期的往里面添加内容。

爬虫工具库-spidertools.cn

协程

模板1(通过loop):

python 复制代码
async def download()
    pass


async def main():
    # 创建任务队列
    tasks = []

    for..........{     
        # 使用for循环调用其它异步函数,添加到任务队列中
        asks.append(asyncio.create_task(download()))
    }

    # 打包提交队列
    await asyncio.gather(*tasks)


if __name__ == '__main__':
    # 创建loop
    loop = asyncio.get_event_loop()

    # 通过loop执行异步函数
    loop.run_until_complete(main())

模板2(通过asyncio):

python 复制代码
async def download()
    pass


async def main():
    # 创建任务队列
    tasks = []

    for..........{     
        # 使用for循环调用其它异步函数,添加到任务队列中
        asks.append(asyncio.create_task(download()))
    }

    # 等待任务结束
    await asyncio.wait(tasks)


if __name__ == '__main__':
    asyncio.run(main()) 

python运行js模板

python 复制代码
import execjs


# 打开本地js文件,读取文件内容后通过execjs.compile函数将其转换成js代码暂存在ctx中
with open('baidu.js', 'r', encoding='utf-8') as f:
    ctx = execjs.compile(f.read())


# 使用 ctx.call 函数来调用JS代码中的函数
# 第一个参数:想要调用的js中的函数
# 第二个参数:传入的值
res = ctx.call('hello', lis)

ffmpeg视频合成

在爬取视频的时候通常是通过m3u8来获取到视频的ts切片,需要把他们全部合成为一个视频切片。

复制代码
def concat():
    video = 'output.mp4'    # 视频输出地址
    ts_locat = './视频切片'      # ts文件路径 

    # 获取所有的 .ts 文件
    ts_files = [f for f in os.listdir(ts_locat) if f.endswith('.ts')]
    ts_files.sort()  # 排序以确保正确的顺序

    # 将排序好的ts文件目录按行依次写入到一个txt文本中
    with open('concat_list.txt', 'w', encoding='utf-8') as f:
        for ts_file in ts_files:
            f.write(f"file '{ts_locat+ '/' +ts_file}'\n")

    # 使用 ffmpeg 读取文本中的ts文件目录地址,按顺序拼接所有的 .ts 文件
    ffmpeg.input('concat_list.txt', format='concat', safe=0).output(video, c='copy').run()
相关推荐
江华森4 小时前
Python 实现高德地图找房(一):环境搭建与数据爬虫
开发语言·爬虫·python
狗都不学爬虫_11 小时前
AI逆向 - 某定制瑞树6纯算(ck-header-params后缀)
爬虫·python·网络爬虫
亿牛云爬虫专家11 小时前
2026架构前沿:将Declarative Crawler(声明式爬虫)引入你的技术栈
爬虫·架构·爬虫代理·requests·隧道代理·声明式爬虫·转发模式
跨境数据猎手13 小时前
反向海淘SaaS系统架构拆解与业务技术
大数据·爬虫·系统架构
跨境观察员小周14 小时前
如何使用Chat GPT进行爬虫?2026年最新教程
爬虫·gpt·chatgpt
喜欢的名字被抢了1 天前
让爬虫请求像真实用户:六项反反爬策略详解
爬虫
跨境数据猎手1 天前
多平台电商比价系统从零搭建合规
开发语言·爬虫·架构
wangruofeng2 天前
68K Star,一个爬虫框架凭什么统一了 HTTP、浏览器和 AI Agent
爬虫·github·ai编程
Bryce学亮3 天前
1688 半自动化拟人浏览信息处理工具爬虫
爬虫·python
简简单单就是我_hehe3 天前
前端埋点日志解决方案:完整 User-Agent 解析工具实现,爬虫识别、设备品牌、操作系统、浏览器版本全解析(附完整源码)
前端·爬虫