B 站真是個神奇的網站。找不到資料了,去 B 站逛一逛,保準有你滿意的東西。
前幾天寫了個爬蟲,用 path、re、BeautifulSoup 爬取的 B 站 python 視頻,如果要爬取多頁的話 在最下方循環中 填寫好循環的次數就可以了
B 站真是個神奇的網站。找不到資料了,去 B 站逛一逛,保準有你滿意的東西。
前幾天寫了個爬蟲,用 path、re、BeautifulSoup 爬取的 B 站 python 視頻,如果要爬取多頁的話 在最下方循環中 填寫好循環的次數就可以了
廢話不多說直接上源碼:
1 '''
2 在學習過程中有什麼不懂得可以加我的
3 python學習交流扣扣qun,688244217
4 羣裏有不錯的學習教程、開發工具與電子書籍。
5 與你分享python企業當下人才需求及怎麼從零基礎學習好python,和學習什麼內容。
6 '''
7 from fake_useragent import UserAgent
8 import requests
9 import time
10
11 ua=UserAgent()
12
13
14 def downloader(url, path):
15 start = time.time() # 開始時間
16 size = 0
17 headers = {
18 'User-Agent':ua.random
19 }
20 response = requests.get(url, headers=headers, stream=True) # stream 屬性必須帶上
21 chunk_size = 1024 # 每次下載的數據大小
22 content_size = int(response.headers['content-length']) # 總大小
23 if response.status_code == 200:
24 print('[文件大小]:%0.2f MB' % (content_size / chunk_size / 1024)) # 換算單位
25 with open(path, 'wb') as file:
26 for data in response.iter_content(chunk_size=chunk_size):
27 file.write(data)
28 size += len(data) # 已下載的文件大小
29 print('\r' + '[下載進度]:%s%.2f%%' % ('>' * int(size * 50 / content_size), float(size / content_size *
30 100)), end=" ")
31 end = time.time() # 結束時間
32 print('\n' + '視頻下載完成!用時%.2f秒' % (end - start))
33
34
35
36 def The_URL(page):
37 URL='http://api.vc.bilibili.com/board/v1/ranking/top?page_size=10&next_offset={}&tag=%E4%BB%8A%E6%97%A5%E7%83%AD%E9%97%A8&platform=pc'.format(page)
38 headers={
39 'User-Agent':ua.random
40 }
41 sponse=requests.get(URL,headers=headers).json()
42 item=sponse.get('data').get('items')
43 for i in item:
44 ite=i.get('item')
45 #視頻標題
46 Video_name=ite.get('description')
47
48 #發佈日期
49 Release_time=ite.get('upload_time_text')
50
51 #視頻下載地址
52 Video_download_link=ite.get('video_playurl')
53
54 #視頻作者
55 The_name=i.get('user').get('name')
56
57 try:
58 print('當前下載的是:%s'%Video_name)
59 downloader(Video_download_link,path='%s.mp4'%Video_name)
60 except Exception as e:
61 print(e.args)
62
63 for i in range(0,100):
64 i=i*10+1
65 The_URL(i)
綜上就是這次的全部內容,多加練習繼續加油!