コード例 #1
0
def parse_prices():
    url = "https://ru.investing.com/indices/mcx-components"
    headers = {
        "User-Agent":
        "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) "
        "Chrome/73.0.3683.86 YaBrowser/19.4.0.2397 Yowser/2.5 Safari/537.36 "
    }
    r = requests.get(url=url, headers=headers)
    sel = Selector(text=r.text)
    companies_info = sel.css('table#cr1 > tbody').xpath(
        './tr[contains(@id,"pair")]')
    for company in companies_info:
        name_el = company.xpath('./td[2]/a')
        name = name_el.xpath('./text()').extract_first()
        current_price = company.xpath(
            './td[contains(@class,"last")]/text()').extract_first()
        current_price = Formatter.format_price(current_price)
        high_price = company.xpath(
            './td[contains(@class,"high")]/text()').extract_first()
        high_price = Formatter.format_price(high_price)
        low_price = company.xpath(
            './td[contains(@class,"low")]/text()').extract_first()
        low_price = Formatter.format_price(low_price)
        volume = company.xpath(
            './td[contains(@class,"turnover")]/text()').extract_first()
        volume = Formatter.format_volume(volume)
        update_time = int(
            company.xpath(
                './td[contains(@class,"time")]/@data-value').extract_first())
        p = Price()
        p.current = current_price
        p.high = high_price
        p.low = low_price
        p.volume = volume
        p.time = datetime.fromtimestamp(update_time)
        company = Company.select().where(Company.name == name).limit(1)
        if len(company) > 0:
            p.company = company[0].id
            p.save()
        else:
            continue
コード例 #2
0
ファイル: data_utils.py プロジェクト: LEVLLN/trades
def save_prices(element_list, stock_code):
    for element in element_list:
        price = Price()
        price.stock = stock_code
        price_date = datetime.date(datetime.strptime(element[0], '%m/%d/%Y'))
        price.date = price_date
        price.open = element[1]
        price.high = element[2]
        price.low = element[3]
        price.close = element[4]
        volume = element[5].replace(',', '')
        if volume == '':
            volume = 0
        price.volume = volume
        try:
            price.save()
            logger.info(
                f'Price object: {price.stock} in date: {price.date} saved in db'
            )
        except Exception as exception:
            logger.error(
                f'{price.code} in date: {price.date} is not saved: {exception}'
            )