聚水潭

2019-12-04  本文已影响0人  Aedda
import requests
import json
import xlwt
import datetime
import tkinter
from threading import Thread


def cookies():
    f = open(u'./cookie.csv')
    cookie = f.read()
    return cookie


def header():
    headers = {
        'Host': u'www.erp321.com',
        'Origin': u'http://www.erp321.com',
        'Referer': u'http://www.erp321.com/app/item/skusn/Search.aspx',
        'User-Agent': u'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.25 Safari/537.36',
        'X-Requested-With': u'XMLHttpRequest',
        'Cookie': cookies(),
    }
    return headers


def formdata():
    shipping_space = cw_13.get()
    start_time = sj_13.get()
    end_time = entry2.get()
    formdata_list = []
    if ',' in str(shipping_space):
        for i in str(shipping_space).split(','):
            FormData = {
                u'__VIEWSTATE': u'/wEPDwULLTEwOTk4MzQ4ODNkZHccuYxiuRZVnHXhJOhPnV2OdsXI',
                u'__VIEWSTATEGENERATOR': '96687167',
                'bin': i,
                'SkuIdIptType': 's1, sku_id',
                'id1': '',
                'id2': '',
                's2': '',
                'remark': '',
                'creater': '',
                'created': start_time,
                'created': end_time,
                'type_name': '',
                'type': '',
                '_jt_page_size': 99999,
                '_cbb_creater': '',
                '__CALLBACKID': 'JTable1',
                '__CALLBACKPARAM': json.dumps({"Method": "LoadDataToJSON", "Args": ['1',
                                                                                    u"[{\"k\":\"bin\",\"v\":\"" + i + u"\",\"c\":\"=\"},{\"k\":\"created\",\"v\":\"" + start_time + u"\",\"c\":\">=\",\"t\":\"date\"},{\"k\":\"created\",\"v\":\"" + end_time + u" 23:59:59.998\",\"c\":\"<=\",\"t\":\"date\"}]",
                                                                                    u"{}"]})
            }
            formdata_list.append(FormData)
    else:
        FormData = {
            '__VIEWSTATE': '/wEPDwULLTEwOTk4MzQ4ODNkZHccuYxiuRZVnHXhJOhPnV2OdsXI',
            '__VIEWSTATEGENERATOR': '96687167',
            'bin': shipping_space,
            'SkuIdIptType': 's1, sku_id',
            'id1': '',
            'id2': '',
            's2': '',
            'remark': '',
            'creater': '',
            'created': start_time,
            'created': end_time,
            'type_name': '',
            'type': '',
            '_jt_page_size': 99999,
            '_cbb_creater': '',
            '__CALLBACKID': 'JTable1',
            "__CALLBACKPARAM": json.dumps({"Method": "LoadDataToJSON", "Args": ['1',
                                                                                "[{\"k\":\"bin\",\"v\":\"" + shipping_space + "\",\"c\":\"=\"},{\"k\":\"created\",\"v\":\"" + start_time + "\",\"c\":\">=\",\"t\":\"date\"},{\"k\":\"created\",\"v\":\"" + end_time + " 23:59:59.998\",\"c\":\"<=\",\"t\":\"date\"}]",
                                                                                "{}"]})
        }
        formdata_list.append(FormData)
    return formdata_list


def res_post():
    data_1 = formdata()
    ls_all = []
    if '' != data_1:
        for http in data_1:
            to_url = 'http://www.erp321.com/app/item/skusn/Search.aspx'
            session = requests.session()
            res = session.post(to_url, data=http, headers=header())
            ls = json.loads(json.loads(res.content.decode()[2:])["ReturnValue"])['datas']
            ls_all += ls
    return ls_all


def spider():
    ls_all = res_post()
    for i in ls_all:
        print()
        for value in i.values():
            print(value, end='-')
    print(u'\n本次查询到的记录共有', len(ls_all), u'条')


def to_exc():
    try:
        book = xlwt.Workbook()
        sheet = book.add_sheet(u"作者qq68110923")
        lsttitle = [u"序号", u"唯一码", u"商品编码", u"数量", u"单据编码", u"单据编码2", u"操作", u"备注", u"仓位", u"箱号", u"备注2", u"操作时间",
                    u"操作人"]
        lie_head = 0
        for lst in lsttitle:
            sheet.write(0, lie_head, lst)
            lie_head += 1
        ls = res_post()
        row = 0
        for i in ls:
            row += 1
            list_value = []
            for value in i.values():
                list_value.append(value)
            lie_v = 0
            for value_v in list_value:
                sheet.write(row, lie_v, value_v)
                lie_v += 1
        nowtime = str(datetime.datetime.now().strftime('%Y%m%d%H%M%S'))
        book.save(u'./qq68110923/' + nowtime + u'.xls')
        print(u'导出成功%s条' % len(ls))
    except:
        print(u'出现未知异常')


def do_btn():
    t = Thread(target=spider)
    t.start()


def to_excel():
    t = Thread(target=to_exc)
    t.start()


if __name__ == '__main__':
    window = tkinter.Tk()
    window.title(u'聚水潭仓库爬虫系统')
    window.geometry('260x180')
    zhuangbi = tkinter.Label(window, text=u"作者QQ:68110923")
    zhuangbi.place(x=1, y=10)
    hint = tkinter.Label(window, text=u"仓位不填为null")
    hint.place(x=1, y=30)

    cw_11 = tkinter.Label(window, text=u"仓位:")
    cw_11.place(x=1, y=60)
    cw_12 = tkinter.StringVar()
    cw_12.set("A-1-3")
    cw_13 = tkinter.Entry(window, textvariable=cw_12)
    cw_13.place(x=40, y=60, width=210)

    sj_11 = tkinter.Label(window, text=u"时间:")
    sj_11.place(x=1, y=90)
    sj_12 = tkinter.StringVar()
    sj_12.set("2019-03-01")
    sj_13 = tkinter.Entry(window, textvariable=sj_12)
    sj_13.place(x=40, y=90, width=100)

    tdate = tkinter.StringVar()
    tdate.set(datetime.datetime.now().strftime("%Y-%m-%d"))
    entry2 = tkinter.Entry(window, textvariable=tdate)
    entry2.place(x=150, y=90, width=100)

    button = tkinter.Button(window, text=u"爬取", command=spider)
    button.place(x=60, y=130)

    button = tkinter.Button(window, text=u"导出excel", command=to_excel)
    button.place(x=140, y=130)
    window.mainloop()
上一篇 下一篇

猜你喜欢

热点阅读