使用Pyrogram发送视频遇WEBPAGE_CURL_FAILED错误求助
解决Pyrogram发送视频触发WEBPAGE_CURL_FAILED错误的方案
1. 修正代码中的明显错误
你当前代码里的url是硬编码的空字符串,这会导致Telegram尝试抓取无效链接,直接触发错误。应改为接收用户发送的消息文本作为目标链接:
from pyrogram import Client, filters from pyrogram.types import Message @client.on_message(filters.text) def send_video_from_url(c: Client, m: Message): url = m.text # 获取用户发送的链接 c.send_video( m.chat.id, url )
2. 针对MP4直链的解决办法
若修正代码后仍报错,大概率是Telegram服务器无法抓取该直链,可尝试以下方案:
- 验证直链有效性:用浏览器直接打开MP4链接,确认能正常下载/播放。若链接失效、需要登录或存在防盗链限制,更换稳定的公开直链。
- 绕过Telegram抓取,本地发送:先将视频下载到本地,再发送本地文件,避免依赖Telegram的URL抓取服务。示例代码:
import requests import os from pyrogram import Client, filters from pyrogram.types import Message @client.on_message(filters.text) def send_video_from_url(c: Client, m: Message): url = m.text temp_file = "temp_video.mp4" try: # 流式下载视频到临时文件 response = requests.get(url, stream=True) response.raise_for_status() with open(temp_file, "wb") as f: for chunk in response.iter_content(chunk_size=8192): f.write(chunk) # 发送本地视频文件 c.send_video(m.chat.id, temp_file) except Exception as e: m.reply_text(f"操作失败:{str(e)}") finally: # 清理临时文件 if os.path.exists(temp_file): os.remove(temp_file)
3. 针对YouTube链接的解决办法
Telegram无法直接解析YouTube网页链接作为视频发送,必须先提取视频直链或下载到本地:
使用yt-dlp工具处理YouTube链接(需先安装:pip install yt-dlp),示例代码:
import yt_dlp import os from pyrogram import Client, filters from pyrogram.types import Message @client.on_message(filters.text) def send_youtube_video(c: Client, m: Message): url = m.text ydl_opts = { 'format': 'best[ext=mp4]', # 选择最佳MP4格式 'outtmpl': 'temp_youtube.mp4', } try: with yt_dlp.YoutubeDL(ydl_opts) as ydl: ydl.download([url]) c.send_video(m.chat.id, "temp_youtube.mp4") except Exception as e: m.reply_text(f"处理YouTube链接失败:{str(e)}") finally: # 清理临时文件 if os.path.exists("temp_youtube.mp4"): os.remove("temp_youtube.mp4")
4. 其他排查点
- 检查运行环境网络:若Pyrogram客户端部署在服务器上,确保服务器能正常访问目标URL(部分服务器可能屏蔽境外站点或特定域名)。
- 避免使用短链/跳转链接:尽量使用直接的视频文件链接,Telegram抓取服务无法处理复杂的跳转逻辑。
内容的提问来源于stack exchange,提问作者Revers_official
相关产品推荐
相关产品推荐

