店播爬取Python脚本

kwaiLiveCookie.py 2.8KB

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