30
2020
12

Python selenium + JS

selenium.webdriver.support.ui WebDriverWait
selenium.webdriver.support expected_conditions EC
selenium webdriver
time



driver = webdriver.Chrome()

driver.maximize_window()

driver.get()

time.sleep()



time.sleep()





time.sleep()
element = driver.find_element_by_xpath()
driver.execute_script(element)


driver.switch_to.default_content()

topic = driver.find_element_by_css_selector()
date = driver.find_element_by_css_selector()
price = driver.find_element_by_css_selector()

change = driver.find_element_by_css_selector()
prating = driver.find_element_by_class_name()
rating = driver.find_element_by_css_selector()


(topic.text++date.text)
(price.text)
(change.text)
(prating.text)
(rating.text)

















# coding=utf-8
import os
import pickle
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
import time


driver = webdriver.Chrome("C:\\Users\\Python\\Desktop\\chromedriver.exe")
driver.maximize_window()


def get_page():
    driver.get("https://www.fidelity.com.hk/zh/funds/factsheet/CFHK/H")

    time.sleep(2)

    element = driver.find_element_by_xpath("/html/body/div[2]/div/div/form/div/div[2]/div[1]")
    driver.execute_script("arguments[0].click();", element)

    time.sleep(1)

    driver.switch_to.default_content()
    elet = driver.find_element_by_xpath("//strong[contains(text(),'您繼續使用本網站前必須細閱上列資料。如按以下「接受」按鈕, 您確認您已閱畢相關風險披露聲明。')]")
    driver.execute_script("arguments[0].click();", elet)
    driver.execute_script("arguments[0].scrollIntoView(true);", elet)


    time.sleep(1)
    element = driver.find_element_by_xpath("//body/div[2]/div[1]/div[1]/form[1]/div[1]/div[3]/div[1]")
    driver.execute_script("arguments[0].click();", element)

def get_data():
    driver.switch_to.default_content()

    topic = driver.find_element_by_css_selector("body.hk-tc:nth-child(2) div.dashboard-bg:nth-child(1) div.main:nth-child(2) div.cmp--factsheet--custom--factsheet-mpf div.cmp--factsheet--custom--general-info div:nth-child(1) div.grid-container.page-padding-top.page-padding-bottom div.grid-x.general-info div.cell.general-info-item > h1:nth-child(1)")
    date = driver.find_element_by_css_selector("body.hk-tc:nth-child(2) div.dashboard-bg:nth-child(1) div.main:nth-child(2) div.cmp--factsheet--custom--factsheet-mpf div.cmp--factsheet--custom--general-info div.grid-container.page-padding-top.page-padding-bottom div.grid-x.general-info div.cell.general-info-item div.cmp--factsheet--custom--nav:nth-child(2) div:nth-child(1) div.grid-x.grid-margin-x.border-line:nth-child(1) div.cell.shrink:nth-child(1) > div.nav-title")
    price = driver.find_element_by_css_selector("body.hk-tc:nth-child(2) div.dashboard-bg:nth-child(1) div.main:nth-child(2) div.cmp--factsheet--custom--factsheet-mpf div.cmp--factsheet--custom--general-info div.grid-container.page-padding-top.page-padding-bottom div.grid-x.general-info div.cell.general-info-item div.cmp--factsheet--custom--nav:nth-child(2) div:nth-child(1) div.grid-x.grid-margin-x.border-line:nth-child(1) div.cell.shrink:nth-child(1) > div.price")

    change = driver.find_element_by_css_selector("body.hk-tc:nth-child(2) div.dashboard-bg:nth-child(1) div.main:nth-child(2) div.cmp--factsheet--custom--factsheet-mpf div.cmp--factsheet--custom--general-info div.grid-container.page-padding-top.page-padding-bottom div.grid-x.general-info div.cell.general-info-item div.cmp--factsheet--custom--nav:nth-child(2) div:nth-child(1) div.grid-x.grid-margin-x.border-line:nth-child(1) div.cell.shrink:nth-child(2) > div.nav-title")
    prating = driver.find_element_by_class_name("change-percentage")
    rating = driver.find_element_by_css_selector("body.hk-tc:nth-child(2) div.dashboard-bg:nth-child(1) div.main:nth-child(2) div.cmp--factsheet--custom--factsheet-mpf div.cmp--factsheet--custom--general-info div.grid-container.page-padding-top.page-padding-bottom div.grid-x.general-info div.cell.general-info-item div.cmp--factsheet--custom--nav:nth-child(2) div.grid-x.grid-margin-x.border-line:nth-child(1) div.cell.shrink:nth-child(2) div.daily-change div.change-absolute span:nth-child(1) > span")


    print(topic.text+":"+date.text)
    print(price.text)
    print(change.text)
    print(prating.text)
    print(rating.text)

    time.sleep(2)




if __name__ == "__main__":
    get_page()
    get_data()

    urls = ['https://www.fidelity.com.hk/zh/funds/factsheet/CFGE/H',
            'https://www.fidelity.com.hk/zh/funds/factsheet/CFAP/H']

    for url in urls:
        driver.get(url)
        time.sleep(2)
        get_data()


« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。