yt_list.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. from selenium import webdriver
  2. from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
  3. import time
  4. import os
  5. import datetime
  6. import urllib.parse
  7. from selenium.webdriver.support.ui import WebDriverWait
  8. from selenium.webdriver.common.by import By
  9. from selenium.webdriver.support import expected_conditions as EC
  10. import codecs
  11. import random
  12. from bs4 import BeautifulSoup
  13. import requests
  14. import time
  15. import rpyc
  16. import sys
  17. import docker
  18. import googlesearch
  19. import codecs
  20. import sys
  21. import time
  22. import dataset
  23. import os
  24. from selenium.webdriver.common.keys import Keys
  25. headers = {
  26. "Authorization": "Bearer " + "t35vhZtWNgvDNWHc3DJh0OKll3mcB9GvC8K2EAkBug2",
  27. "Content-Type": "application/x-www-form-urlencoded"
  28. }
  29. def send_msg(kw):
  30. params = {"message": "處理關鍵字: "+kw}
  31. r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params=params)
  32. def empty_query(q):
  33. global driver
  34. googleurl='https://www.google.com/search?q='+urllib.parse.quote(q)
  35. driver.get(googleurl)
  36. time.sleep(3)
  37. def scrolling(driver,pgnum):
  38. ub = driver.find_element_by_css_selector('body')
  39. for i in range(pgnum):
  40. ub.send_keys(Keys.PAGE_DOWN)
  41. if pgnum>1:
  42. time.sleep(0.3)
  43. def process_query(qs,number_results=10,language_code='en',pat='hhh.com.tw'):
  44. global driver
  45. escaped_search_term=urllib.parse.quote(qs)
  46. # escaped_search_term = qs.replace(' ', '+')
  47. # googleurl='https://www.google.com/search?q='+
  48. googleurl = 'https://www.google.com/search?q={}&num={}&hl={}'.format(escaped_search_term, number_results+1,language_code)
  49. driver.get(googleurl)
  50. elmts=driver.find_elements_by_xpath("//div[@class='yuRUbf']/a")
  51. idx=0
  52. for elmt in elmts:
  53. try:
  54. href=elmt.get_attribute('href')
  55. print(str(idx)+': '+href)
  56. if pat in href:
  57. return idx
  58. idx+=1
  59. except:
  60. print('href exception')
  61. try:
  62. elmt=driver.find_element_by_xpath("//a[@id='pnnext']")
  63. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  64. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  65. except:
  66. print('pnnext exception')
  67. return None
  68. time.sleep(4)
  69. elmts=driver.find_elements_by_xpath("//div[@class='yuRUbf']/a")
  70. for elmt in elmts:
  71. try:
  72. href=elmt.get_attribute('href')
  73. print(str(idx)+': '+href)
  74. if pat in href:
  75. return idx
  76. idx+=1
  77. except:
  78. print('href2 exception')
  79. result=[]
  80. driver=None
  81. def restart_browser():
  82. options = webdriver.ChromeOptions()
  83. # options.add_argument("--proxy-server=socks5://130.61.93.198:1080")
  84. driver=webdriver.Chrome(desired_capabilities=options.to_capabilities())
  85. driver.set_window_size(1400,1000)
  86. return driver
  87. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
  88. table=db['yt_list']
  89. driver=restart_browser()
  90. driver.get('https://www.youtube.com/c/%E5%B9%B8%E7%A6%8F%E7%A9%BA%E9%96%93gorgeousspace/videos?view=0&sort=dd&flow=grid')
  91. time.sleep(1)
  92. scrolling(driver,60)
  93. try:
  94. elmts=driver.find_elements_by_xpath("//a[@id='video-title']")
  95. for elmt in elmts:
  96. url=elmt.get_attribute('href')
  97. url=url.replace('https://www.youtube.com/watch?v=','')
  98. table.insert({'folder':'upload','video':url})
  99. except:
  100. print('pnnext exception')