Skip to content

Instantly share code, notes, and snippets.

@alexisperrier
Last active June 23, 2024 08:41
Show Gist options
  • Select an option

  • Save alexisperrier/952a0c34b92ef76c761abdb4a9633f0d to your computer and use it in GitHub Desktop.

Select an option

Save alexisperrier/952a0c34b92ef76c761abdb4a9633f0d to your computer and use it in GitHub Desktop.
Obtenir les ids des videos d'un compte tiktok avec yt-dlp
'''
Code généré par Claude.ai en juin 2024.
'''
import yt_dlp
import asyncio
async def get_tiktok_video_ids(username, limit=20):
ydl_opts = {
'extract_flat': True,
'force_generic_extractor': True,
'quiet': True,
'playlist_items': f'1-{limit}' # Limit to the first 'limit' videos
}
url = f'https://www.tiktok.com/@{username}'
loop = asyncio.get_event_loop()
video_ids = []
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = await loop.run_in_executor(None, ydl.extract_info, url, False)
if 'entries' in info:
for entry in info['entries']:
if 'id' in entry:
video_ids.append(entry['id'])
if len(video_ids) >= limit:
break
except Exception as e:
print(f"Error fetching video IDs for {username}: {str(e)}")
return video_ids
async def main():
username = 'cnews' # Replace with the TikTok username you're interested in
video_ids = await get_tiktok_video_ids(username, limit=20)
print(f"Found {len(video_ids)} videos for @{username}")
for vid in video_ids:
print(f"https://www.tiktok.com/@{username}/video/{vid}")
if __name__ == "__main__":
asyncio.run(main())
@alexisperrier
Copy link
Author

La fonction get_tiktok_video_ids prend un nom d'utilisateur TikTok.

Les options de yt-dlp :

  • 'extract_flat': True : indique à yt-dlp d'extraire uniquement les informations des vidéos sans les télécharger.
  • 'force_generic_extractor': True : utiliser l'extracteur générique, qui est souvent plus fiable pour TikTok.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment