diff --git a/one.py b/one.py new file mode 100644 index 0000000..eda73ef --- /dev/null +++ b/one.py @@ -0,0 +1,22 @@ +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