Last active
November 1, 2018 01:36
-
-
Save popowa/7e3aade6cfc45ae5582bf6d205779a39 to your computer and use it in GitHub Desktop.
自分が欲しかったので
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import time | |
from bs4 import BeautifulSoup | |
from os import path, sys | |
from datetime import date, datetime | |
from weasyprint import HTML | |
""" | |
What's This? | |
SPIRALをコンテンツをPDFにしてローカルに保存してくれるスクリプト | |
Install方法: (pyenvですでにPython3.5+が入っている前提。3.3+であれば動くと思う) | |
pip install BeautifulSoup, weasyprint | |
brew install cairo pango gdk-pixbuf libffi | |
TODO: | |
賞与が出た時の分岐がHTML上判別が出来ないので、後でなんか考える | |
""" | |
#設定 | |
dirname = path.dirname(__file__) | |
login_info = { | |
'fフォームのID': '', #社員番号を入れるフォームのIDとその番号 | |
'pフォームのPassword': '', #パスワードを入れるフォームのIDとそのパスワード | |
'SMPAREA':'' #URLの引数とformに埋め込みの引数が変わったので要素確認をしてformに埋め込みしてある方に変える | |
} | |
url_original = 'https://areaXX.smp.ne.jp' #自社のURLを確認する,areaXXのXXを数字に変えればよい | |
url_login = 'https://areaXX.smp.ne.jp/area/Login' | |
options = { | |
'quiet': '', | |
'encoding': "UTF-8", | |
'page-size': 'A4', | |
} | |
session = requests.Session() | |
res = session.post(url_login, data=login_info) | |
res.raise_for_status() | |
today = date.today() | |
download_month = "{}年{}月".format(today.year, today.month) | |
#download_month = "2017年11月" #当月分が出てない時、ハードコーディング指定 | |
download_filename = "{}.pdf".format(download_month) | |
html = BeautifulSoup(res.text,"html.parser") | |
for a in html.find_all("a"): | |
if download_month in html.text and download_month == a.text: | |
print('find payslip. start to fetch') | |
download_url = url_original + a.get('href') | |
detail_html = session.get(download_url) | |
if detail_html.status_code == 200: | |
print('start to make it as PDF') | |
html2pdf = HTML(string=detail_html.text.replace('Shift_JIS', 'UTF-8')) | |
html2pdf.write_pdf(download_filename) | |
else: | |
print('ERROR') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment