|
| 1 | +# @Time : 2020/3/18 13:00 |
| 2 | +# @Author : Libuda |
| 3 | +# @FileName: main_spider.py |
| 4 | +# @Software: PyCharm |
| 5 | +import requests |
| 6 | +import datetime |
| 7 | +import csv |
| 8 | +import json |
| 9 | +import xlrd |
| 10 | +from xlutils.copy import copy |
| 11 | + |
| 12 | +base_url = "https://pk10tv.com/pks/getPksHistoryList.do?date={}&lotCode=xyft" |
| 13 | +start_date = datetime.datetime(2020, 3, 15) # 起始时间 |
| 14 | +end_date = datetime.datetime(2020, 3, 18) # 结束时间 |
| 15 | +txt_name = "res.csv" # 保存csv的路径 |
| 16 | +# 时间间隔 每隔一天爬一次就可以 |
| 17 | +delta_date = datetime.timedelta(hours=24) |
| 18 | + |
| 19 | + |
| 20 | +# start_stamp = start_date |
| 21 | +# end_stamp = start_date |
| 22 | +def get_keywords_data(tables, row, col): |
| 23 | + actual_data = tables.cell_value(row, col) |
| 24 | + return actual_data |
| 25 | + |
| 26 | + |
| 27 | +def write_to_excel(file_path, row, col, value): |
| 28 | + work_book = xlrd.open_workbook(file_path, formatting_info=False) |
| 29 | + write_to_work = copy(work_book) |
| 30 | + sheet_data = write_to_work.get_sheet(0) |
| 31 | + sheet_data.write(row, col, str(value)) |
| 32 | + write_to_work.save(file_path) |
| 33 | + |
| 34 | + |
| 35 | +# 表头 |
| 36 | +with open(txt_name, 'a+', newline='') as f: |
| 37 | + writer = csv.writer(f) |
| 38 | + row_item = ["" for _ in range(3)] |
| 39 | + row_item[0] = "时间" |
| 40 | + row_item[1] = "期数" |
| 41 | + row_item[2] = "开奖结果" |
| 42 | + writer.writerow(row_item) |
| 43 | + |
| 44 | +while start_date <= end_date: |
| 45 | + url = base_url.format(start_date.strftime("%Y-%m-%d")) |
| 46 | + response = requests.get(url).text |
| 47 | + response = json.loads(response) |
| 48 | + datas = response['result']['data'][::-1] |
| 49 | + |
| 50 | + for one in datas: |
| 51 | + filter_time = map(int, one['preDrawTime'].split(" ")[0].split("-")) |
| 52 | + if datetime.datetime(*filter_time) > end_date: |
| 53 | + continue |
| 54 | + with open(txt_name, 'a', newline='') as f: |
| 55 | + writer = csv.writer(f) |
| 56 | + # 需要写入多少列就循环多少次 |
| 57 | + row_item = ["" for _ in range(3)] |
| 58 | + row_item[0] = one['preDrawTime'] |
| 59 | + row_item[1] = one['preDrawIssue'] |
| 60 | + row_item[2] = one['preDrawCode'] |
| 61 | + # print(row_item) |
| 62 | + writer.writerow(row_item) |
| 63 | + start_date += delta_date |
| 64 | + |
| 65 | +print("ok") |
0 commit comments