店播爬取Python脚本

kwaiLiveCookieMulti.py 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # coding=utf-8
  2. import os
  3. from selenium import webdriver
  4. import time
  5. import requests
  6. import sys
  7. import warnings
  8. warnings.filterwarnings("ignore")
  9. '''
  10. 记录日志
  11. '''
  12. def print_log(content):
  13. content = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ' ' + content
  14. print(content)
  15. # logfile = './kwaiLiveCookie.log'
  16. # file = open(logfile, 'a', encoding='utf8')
  17. #
  18. # file.write(content + '\n')
  19. # # 关闭文件
  20. # file.close()
  21. class KwaiLiveWebDriver:
  22. # 初始化方法:设置浏览器驱动
  23. def __init__(self, phone):
  24. chrome_driver_path = r'chromedriver.exe'
  25. option = webdriver.ChromeOptions()
  26. option.add_argument(r'--user-data-dir=D:\Chrome\\' + phone) # 设置成用户自己的数据目录
  27. option.add_argument(r'--disable-software-rasterizer')
  28. os.environ["webdriver.chrome.driver"] = chrome_driver_path
  29. self.browser = webdriver.Chrome(
  30. executable_path=chrome_driver_path,
  31. options=option
  32. )
  33. def run(self):
  34. browser = self.browser
  35. self.openBrowser()
  36. print_log('打开浏览器,进入快手直播页')
  37. browser.implicitly_wait(60) # 隐性等待,最长等30秒
  38. self.openLive()
  39. time.sleep(60)
  40. self.sendCookie()
  41. time.sleep(300)
  42. browser.quit()
  43. print_log('关闭浏览器')
  44. print_log('--------------------------------')
  45. def openBrowser(self):
  46. browser = self.browser
  47. # browser.maximize_window()
  48. # chrome浏览器打开快手直播页
  49. browser.get("https://live.kuaishou.com/")
  50. def retryOpenLive(self, retry=0):
  51. browser = self.browser
  52. # 主播尚未开播,则点击推荐直播
  53. if "主播尚未开播" in browser.page_source:
  54. if retry >= 10:
  55. print_log('主播尚未开播,重试失败:' + str(retry) + ' ' + browser.current_url)
  56. browser.quit()
  57. print_log('关闭浏览器')
  58. print_log('--------------------------------')
  59. return
  60. print_log('主播尚未开播,重试:' + str(retry) + ' ' + browser.current_url)
  61. previewLive = browser.find_elements_by_class_name('preview-video')
  62. previewLiveLen = len(previewLive)
  63. if previewLiveLen > 0:
  64. print(previewLiveLen)
  65. previewLive[previewLiveLen - 1].click()
  66. browser.refresh()
  67. time.sleep(5)
  68. retry = retry + 1
  69. return self.retryOpenLive(retry)
  70. def openLive(self):
  71. browser = self.browser
  72. browser.implicitly_wait(60) # 隐性等待,最长等30秒
  73. enterLive = browser.find_element_by_class_name('enter-live-btn')
  74. # 点击进入直播间
  75. enterLive.click()
  76. time.sleep(10)
  77. self.retryOpenLive()
  78. print_log('获取当前链接:' + browser.current_url)
  79. def sendCookie(self):
  80. browser = self.browser
  81. cookies_list = browser.get_cookies()
  82. print_log('获取Cookie:')
  83. # print(cookies_list)
  84. cookies_str = ''
  85. for cookie_item in cookies_list:
  86. cookies_str = cookies_str + cookie_item['name'] + '=' + cookie_item['value'] + '; '
  87. print_log(cookies_str)
  88. if 'userId' not in cookies_str:
  89. browser.quit()
  90. print_log('关闭浏览器')
  91. print_log('--------------------------------')
  92. return
  93. response = requests.post(
  94. 'https://ks.wenxingshuju.com/v2/api/kwaiOpen/saveCookie',
  95. {'cookie': cookies_str}
  96. )
  97. print_log('发送Cookie:' + response.text)
  98. if __name__ == '__main__':
  99. phone = sys.argv[1]
  100. kwaiLiveWebDriver = KwaiLiveWebDriver(phone)
  101. try:
  102. kwaiLiveWebDriver.run()
  103. except Exception as e:
  104. print_log('抛出异常!' + str(e))
  105. kwaiLiveWebDriver.browser.quit()
  106. sys.exit(0)