message_coin_current_price.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. '''
  2. 获取 coin 现价, 并发邮件提醒
  3. (先做单个coin)
  4. '''
  5. import datetime
  6. import sys
  7. import os
  8. import time
  9. from playwright.sync_api import sync_playwright
  10. sys.path.append(os.path.join(os.path.abspath(__file__).split('auto')[0] + 'auto'))
  11. from utils.utils_load_config import load_config, get_base_path
  12. config_json = load_config()
  13. base_project = get_base_path()
  14. from playwright.sync_api import sync_playwright
  15. import time
  16. class CheckCoinCurrentPrice(object):
  17. def __init__(self):
  18. self.number_of_reminders = 5
  19. self.reminder_interval = 60 * 5
  20. self.target_price = 2.04
  21. def main(self):
  22. coins_link = {
  23. 'SUI': 'https://www.528btc.com/coin/33118.html'
  24. }
  25. url = coins_link['SUI']
  26. with sync_playwright() as playwright:
  27. browser = playwright.webkit.launch(headless=False)
  28. context = browser.new_context(viewport={'width': 1920, 'height': 1080})
  29. page = context.new_page()
  30. # 使用选择器获取元素的文本内容
  31. selector = '#detail_app_cid_5113 > div.money > div.sum > i > i.price_num.wordRise'
  32. while True:
  33. try:
  34. page.goto(url)
  35. page.wait_for_load_state('load')
  36. time.sleep(3) # 确保页面完全加载
  37. element = page.query_selector(selector)
  38. if element:
  39. price_text = element.inner_text()
  40. print(f'SUI price: {price_text}' + '\t' + datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
  41. else:
  42. print("Element not found")
  43. context.close()
  44. browser.close()
  45. exit(0)
  46. if float(price_text) > self.target_price:
  47. break
  48. except Exception as e:
  49. print(e)
  50. exit(0)
  51. # 关闭浏览器
  52. context.close()
  53. browser.close()
  54. if __name__ == '__main__':
  55. C = CheckCoinCurrentPrice()
  56. C.main()