list_channel_YTcrawl - 複製.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import csv
  2. from youtubesearchpython import *
  3. from tqdm import tqdm
  4. import time
  5. pages = 50
  6. max_channels = 8 * pages
  7. search = ChannelsSearch('微電影', limit = 8)
  8. c_list = []
  9. print('Filtering Channels................')
  10. for p in tqdm(range(pages)):
  11. for channel in search.result()['result']:
  12. if channel['subscribers'] is None:
  13. continue
  14. subscribes = channel['subscribers'].replace(' subscribers','').replace(' subscriber','')
  15. if 'K' in subscribes:
  16. subscribes = subscribes.replace('K','')
  17. if '.' in subscribes :
  18. subscribes=subscribes.replace('.','')
  19. subscribes+='00'
  20. else:
  21. subscribes+='000'
  22. if 'M' in subscribes:
  23. subscribes = subscribes.replace('M','')
  24. if '.' in subscribes :
  25. zeros = len(subscribes)-subscribes.index('.')-1
  26. subscribes=subscribes.replace('.','')
  27. subscribes += '000000'[:6-zeros]
  28. if int(subscribes)>100:
  29. c_list.append(channel)
  30. search.next()
  31. print('There is ',len(c_list),' channels')
  32. v_list = []
  33. for ch in c_list:
  34. channel_id = ch['id']
  35. try:
  36. search = ChannelSearch('*', channel_id)
  37. result = search.result()['result']
  38. for r in result:
  39. print(r.keys())
  40. print(r['channel']['name'])
  41. except:
  42. print(channel_id)
  43. '''
  44. with open('youtubeReport.csv', 'w', newline='') as csvfile:
  45. writer = csv.writer(csvfile)
  46. writer.writerow(['channel', 'title', '體重'])
  47. writer.writerow(['令狐沖', 175, 60])
  48. for r in results:
  49. print(r.keys())
  50. print(r['title'])
  51. '''