import requests from bs4 import BeautifulSoup def get_video_ids(keyword, num_videos): video_ids = [] page_num = (num_videos - 1) // 20 + 1 base_url = "https://search.bilibili.com/all?keyword={}&order=totalrank&duration=0&tids_1=0&page={}" for page in range(1, page_num + 1): url = base_url.format(keyword, page) response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') video_tags = soup.find_all('li', {'class': 'video-item matrix'}) for tag in video_tags: video_id = tag.get('data - aid') if video_id: video_ids.append(video_id) if len(video_ids) >= num_videos: break if len(video_ids) >= num_videos: break return video_ids