python扒支付记录学到的东西

年底了,一年没有好好记账了,差不多该统计下今年有多么的入不敷出了。我用的Money Pro来记账,可以导入csv文件。银行、某宝都可以下载支付记录的,但是某信不可以,只好自己动手。上班时间,抽了两个下午,边google边写,总算是搞定了。

Python只拿来写过算法题,没有写过这种接近项目的东西,于是学习了一堆很基本的东西。姑且记录一下。

requests库

r = requests.get(url, params=params, cookies=cookies) # how to send with cookies
headers = {'Content-type': 'application/x-www-form-urlencoded'}
r = requests.post(url, params=params, headers=headers, cookies=cookies) # how to send with headers
result = r.text

How to throw an exception

raise Exception('Lack of Cookie: ' + key)

How to write to a file

打开文件,不存在则新建,写入并append,支持utf-8,并且会及时写入文件。

def log(text):
    with open('log.txt', 'a+', newline='', encoding='utf-8-sig') as logfile:
        logfile.write(str(text) + 'n')

How to convert json to dictionary

json.loads(ajson)

How to write to csv

def write_csv(record, count):
    with open('payroll.csv', 'a+', newline='', encoding='utf-8-sig') as csvfile:
        csvwriter = csv.writer(csvfile, delimiter=',', quotechar=',', quoting=csv.QUOTE_MINIMAL)
        if count == 0:
            csvwriter.writerow(record[0].keys())
        for roll in record:
            row = []
            for v in roll.values():
                text = u''.join(str(v))
                row.append(text)
            csvwriter.writerow(row)

u''.join(astring)encoding='utf-8-sig' 则不报错

UnicodeDecodeError: 'ascii' codec can't decode byte 0x80 in position 0:
ordinal not in range(128)
每个loop的文件没有及时写入文件怎么办?
csvfile.flush()

References:

  • http://docs.python-requests.org/en/master/user/quickstart
  • https://docs.python.org/3.3/tutorial/inputoutput.html#reading-and-writing-files
  • https://docs.python.org/3.5/library/csv.html
  • https://docs.python.org/3.5/howto/unicode.html#the-string-type
  • https://stackoverflow.com/questions/9824806/how-come-a-file-doesnt-get-written-until-i-stop-the-program
  • https://docs.python.org/3.5/library/json.html
  • http://www.jianshu.com/p/87b60b696780
  • https://stackoverflow.com/questions/2052390/manually-raising-throwing-an-exception-in-python