# coding: utf-8
import re
from math import ceil
from datetime import datetime
import requests
from lxml import etree
tg = '癸甲乙丙丁戊己庚辛壬' # 天乾字串
dz = '亥子丑寅卯辰巳午未申酉戌' # 位址字串
def to_ganzhi(year, mon, day, hour=1, min=1, sec=1):
"""干支紀年轉換
:param year: 公曆年, int 2018
:param mon: 公曆月, int 1
:param day: 公曆日, int 1
:param hour: 24進製小時, int 1
:param min: 分鐘, int 1
:param sec: 秒, int 1
:return: 干支紀年法表示的時間, xx年xx月xx日xx時
"""date_time = datetime.strptime(
"{}-- ::".format(year, mon, day, hour, min, sec), "%y-%m-%d %h:%m:%s")
lichun = get_jieqi(year)
jieqi = get_jieqi(year, mon)
year4compute = year - 1 if date_time < lichun else year
mon4compute = (mon + 11) % 12 if date_time < jieqi else mon
# 年干支
year_g = (year4compute+7) % 10 # 年幹
year_z = (year4compute + 9) % 12 # 年支
# 月干支
mon_g = (year % 5*2 + mon4compute + 3) % 10 # 月幹
mon_z = (mon4compute + 1) % 12 # 月支
# 日干支
date_init = datetime.strptime("0001-01-05", "%y-%m-%d")
date = datetime.strptime("{}--".format(year, mon, day), "%y-%m-%d")
dete = (date - date_init).days
dete4compute = dete + 1 if hour >= 23 else dete
day_g = dete4compute % 10 # 日乾
day_z = (dete4compute + 8) % 12 # 日支
# 時干支
hour_g = int((dete4compute % 5 * 2 + ceil(hour/2) + 9) % 10) # 時乾
hour_z = int((ceil(hour/2) + 1) % 12) # 時支
print("args_g", year_g, mon_g, day_g, hour_g)
print("args_z", year_z, mon_z, day_z, hour_z)
print(type(hour_z))
gz = "{}{}年 {}{}月 {}{}日 {}{}時".format(
tg[year_g],
dz[year_z],
tg[mon_g],
dz[mon_z],
tg[day_g],
dz[day_z],
tg[hour_g],
dz[hour_z]
)print(gz)
def get_jieqi(start_year, mon=2, limit=1):
"""獲取節氣時間, 如果要存庫,可以傳入limit引數
::param start_year: 起始年份
:param limit:需要獲取的年數
:return: 資料生成器
"""jieqi =
headers =
if limit == 1:
url = "" % (start_year, jieqi[mon])
response = requests.get(url=url, headers=headers)
html = etree.html(response.text)
ret = html.xpath('//div[@class="jieqi_neirong_biaoti_beizhu"]/strong/text()')[0]
data = re.sub(r"[年月]", "-", ret).replace("日", "")
time_data = datetime.strptime(data, "%y-%m-%d %h:%m:%s")
print(time_data, type(time_data))
return time_data
else:
time_list =
urls = ("" % year for year in range(
start_year, start_year + limit))
for url in urls:
response = requests.get(url=url, headers=headers)
html = etree.html(response.text)
ret = html.xpath('//div[@class="jieqi_neirong_biaoti_beizhu"]/strong/text()')[0]
data = re.sub(r"[年月]", "-", ret).replace("日", "")
time_data = datetime.strptime(data, "%y-%m-%d %h:%m:%s")
print(time_data, type(time_data))
time_gen = iter(time_list)
year_gen = range(start_year, start_year + limit)
return year_gen, time_gen
if __name__ == '__main__':
to_ganzhi(2013, 12, 2, 23, 11, 10) # 示例
風火程式設計 requests爬蟲的使用
import request request.get url url,headers from lxml import etree item html etree.html response.text title html.xpath h4 text if title item title titl...
風火程式設計 python中的字典, 對無序的理解
字典是無序的,無序即不是序列,不能通過索引取值.但是鍵是有順序的.鍵的順序是根據加入字典的時序排列的.adict.keys 的型別是dice key,是雜湊形式存在的物件,不是序列,也不能通過索引取值,但可以轉成列表 元組 集合等序列.demot 1,2 11,22 111,222 def func...
Linux網路程式設計 4 完整的讀寫函式
一旦我們建立了連線,我們的下一步就是進行通訊了.在linux下面把我們前面建立的通道 看成是檔案描述符,這樣伺服器端和客戶端進行通訊時候,只要往檔案描述符裡面讀寫東西了.就象我們往檔案讀寫一樣.4.1 寫函式write ssize t write int fd,const void buf,size...