python nav_2018

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python nav_2018相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env python3

import os
import sys
import time
import requests
import lxml.html as lxhtml
import logging
import openpyxl

logging.basicConfig(format="%(asctime)s  %(filename)s:%(lineno)d  %(message)s",
                    datefmt="%Y-%m-%d  %H:%M.%S", level=logging.DEBUG,
                    filename=f'{os.path.dirname(__file__)}/nav_2018.log')


def read_data_from_site():
    url = "https://2018.navalny.com"
    common_path = ".branches-map__statistic--desktop .branches-map__value"

    page = requests.get(url)
    parser = lxhtml.fromstring(page.text)

    span = []

    for elem in parser.cssselect(common_path):
        span += elem.cssselect("span")

    podpisi = span[0].text
    volonteers = span[1].text
    headquoters = span[2].text

    podpisi = "".join(podpisi.split())
    volonteers = "".join(volonteers.split())
    headquoters = "".join(headquoters.split())

    logging.info(f' read: {podpisi}, {volonteers}, {headquoters}')
    return podpisi, volonteers, headquoters


def write_data_to_xls(sheet_name, podpisi, volonteers, headquoters):
    path = f'{os.path.dirname(__file__)}/xls/nav_2018.xlsx'

    if not os.path.exists(path):
        logging.info(f'No file: "{path}"')
        exit(1)

    try:
        book = openpyxl.load_workbook(filename=path)
    except Exception as e:
        logging.info(f'Error in "book = openpyxl.load_workbook(filename=path)" - {e}')
        exit(1)

    sheet_names = book.get_sheet_names()

    if sheet_name not in sheet_names:
        logging.info(f'No sheet "{sheet_name}" in "{sheet_names}"')
        exit(1)

    sheet = book.get_sheet_by_name(sheet_name)

    for row in range(2, 11000):
        if sheet["B" + str(row)].value is None:
            break
        else:
            pass

    sheet["A" + str(row)].value = time.strftime("%d/%m/%Y %H:%M")
    sheet["B" + str(row)].value = int(podpisi)
    sheet["E" + str(row)].value = int(volonteers)
    sheet["H" + str(row)].value = int(headquoters)

    book.save(path)
    book.close()

    logging.info(f'write: {podpisi}, {volonteers}, {headquoters} in row {row}')


def main():
    logging.info('--------------------')

    if len(sys.argv) > 1:
        podpisi, volonteers, headquoters = read_data_from_site()

        if sys.argv[1] == "hour":
            write_data_to_xls("hour", podpisi, volonteers, headquoters)
        elif sys.argv[1] == "day":
            write_data_to_xls("day", podpisi, volonteers, headquoters)


if __name__ == "__main__":
    main()

以上是关于python nav_2018的主要内容,如果未能解决你的问题,请参考以下文章

传递 SOAP req:传递 ItemNo String 但它无法找到在 NAV 2018 中作为 Web 服务公开的 Codeunit

Python基础之python代码程序内存回收机制

Javascript:Marcar NavegaciÃn(各种项目)

python测试开发django-153.bootstrap导航-标签页切换(nav-tabs)

Python基础之python注释方式

固定导航(Sticky nav)