#Config.py
MONGO_URL = 'localhost'
MONGO_DB = 'tuerqi'
MONGO_COLLECTION = 'data'
#history.py
from config import *
import pymongo
from lxml import etree
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
# Chrome Headless 模式
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
browser = webdriver.Chrome(chrome_options=chrome_options)
browser.get('https://m.cn.investing.com/etfs/ishares-msci-turkey-historical-data')
# 获取源码
result = browser.page_source
html = etree.HTML(result)
client = pymongo.MongoClient(MONGO_URL)
db = client[MONGO_DB]
# 获取历史数据
def get_data():
try:
items = html.xpath('//*[@class="js-history-data"]/tr')
for item in items:
text = item.xpath('./td/text()')
data = {
'日期': text[0],
'收盘': text[1],
'开盘': text[2],
'高': text[3],
'低': text[4],
'交易量': text[5],
'百分比': text[6]
}
save_to_mongo(data)
except TimeoutException:
get_data()
# 存储到MongoDB
def save_to_mongo(data):
try:
if db[MONGO_COLLECTION].insert(data):
print('存储到mongodb成功', data)
except Exception:
print('存储失败')
if __name__ == '__main__':
get_data()

土耳其

土耳其
网友评论