python多线程爬取视频
时间: 2023-09-07 13:16:44 浏览: 127
如果你想要加速视频爬取的过程,可以使用Python的多线程技术来同时进行多个视频的爬取。下面是一个使用多线程爬取Bilibili视频的示例代码:
```python
import requests
import json
from threading import Thread
def get_bilibili_videos(keyword):
url = f"https://api.bilibili.com/x/web-interface/search/all/v2?keyword={keyword}"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
'Referer': 'https://www.bilibili.com/',
}
response = requests.get(url, headers=headers)
data = json.loads(response.text)
videos = []
for item in data['data']['result']:
title = item['title']
url = f"https://www.bilibili.com/video/{item['bvid']}"
videos.append({'title': title, 'url': url})
return videos
def crawl_videos(keyword):
videos = get_bilibili_videos(keyword)
for video in videos:
print(f"标题:{video['title']}")
print(f"链接:{video['url']}")
print("-------------------")
keyword = input("请输入要搜索的关键字:")
threads = []
for i in range(5): # 设置线程数量
t = Thread(target=crawl_videos, args=(keyword,))
threads.append(t)
t.start()
for t in threads:
t.join()
```
这个示例代码创建了5个线程,并且每个线程都调用 `crawl_videos` 函数来爬取视频信息。你可以根据需要调整线程的数量。注意,在多线程爬取时,要注意网站的访问频率限制,避免给网站带来过大的负载。
阅读全文