Administrator
2023-02-16 92cb2dd75ea37b64b174f42ddd0b5b17d6a4634a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
热门板块监听
"""
import logging
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
 
 
def __parseData(driver):
    items = driver.find_element(by=By.ID, value="nuxt-layout-container").find_element(by=By.CLASS_NAME,
                                                                                      value="topgainer-content-left").find_elements(
        by=By.CLASS_NAME,
        value="topgainer-tag")
    data_list = []
    for item in items:
        print("----------------------")
        header = item.find_element(by=By.TAG_NAME, value="section").find_element(by=By.TAG_NAME, value="header")
        title = header.find_element(by=By.TAG_NAME, value="h3").text
        total_rate = header.find_element(by=By.TAG_NAME, value="span").text
        print(title, total_rate)
        contents = item.find_element(by=By.TAG_NAME, value="div").find_element(by=By.TAG_NAME,
                                                                               value="tbody").find_elements(
            by=By.TAG_NAME, value="tr")
        codes_list = []
        for content in contents:
            tds = content.find_elements(by=By.TAG_NAME, value="td")
            code = tds[0].find_elements(by=By.TAG_NAME, value="span")[1].text
            limit_up_info = tds[1].text
            price = tds[2].text
            rate = tds[3].text
            limit_up_time = tds[4].text
            huanshou = tds[5].text
            ltsz = tds[6].text
            codes_list.append((code, limit_up_info, price, rate, limit_up_time, huanshou, ltsz))
        data_list.append((title, total_rate, codes_list))
        print("----------------------")
 
    return data_list
 
 
# 获取热门板块
def get_hot_block(callback):
    # 先启动浏览器
    options = Options()
    options.add_argument("--disable-blink-features")
    options.add_argument("--disable-blink-features=AutomationControlled")
    driver = webdriver.Chrome(options=options)
    driver.get("https://xuangubao.cn/top-gainer")
    time.sleep(5)
    while True:
        time.sleep(3)
        try:
            result = __parseData(driver)
            callback(result)
        except Exception as e:
            logging.exception(e)