|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +@author: 闲欢 |
| 5 | +""" |
| 6 | + |
| 7 | +import requests |
| 8 | +import time |
| 9 | +import random |
| 10 | +import openpyxl |
| 11 | + |
| 12 | + |
| 13 | +# 分页获取商品 |
| 14 | +def get_premium_offer_list(keyword, page): |
| 15 | + offer_list = [] |
| 16 | + for i in range(1, int(page) + 1): |
| 17 | + time.sleep(random.randint(0, 10)) |
| 18 | + olist = get_page_offer(keyword, i) |
| 19 | + offer_list.extend(olist) |
| 20 | + return offer_list |
| 21 | + |
| 22 | +# 获取一页商品 |
| 23 | +def get_page_offer(keyword, pageNo): |
| 24 | + url = "https://data.p4psearch.1688.com/data/ajax/get_premium_offer_list.json?beginpage=%d&keywords=%s" % (pageNo, keyword) |
| 25 | + res = requests.get(url) |
| 26 | + result = res.json() |
| 27 | + offerResult = result['data']['content']['offerResult'] |
| 28 | + result = [] |
| 29 | + for offer in offerResult: |
| 30 | + obj = {} |
| 31 | + # print(offer['attr']['id']) |
| 32 | + obj['id'] = str(offer['attr']['id']) |
| 33 | + # print(offer['title']) |
| 34 | + obj['title'] = str(offer['title']).replace('<font color=red>', '').replace('</font>', '') |
| 35 | + # print(offer['attr']['company']['shopRepurchaseRate']) |
| 36 | + obj['shopRepurchaseRate'] = str(offer['attr']['company']['shopRepurchaseRate']) |
| 37 | + # print(offer['attr']['tradeQuantity']['number']) |
| 38 | + obj['tradeNum'] = int(offer['attr']['tradeQuantity']['number']) |
| 39 | + obj['url'] = str(offer['eurl']) |
| 40 | + result.append(obj) |
| 41 | + |
| 42 | + return result |
| 43 | + |
| 44 | +# 写Excel |
| 45 | +def write_excel_xlsx(path, sheet_name, value): |
| 46 | + index = len(value) |
| 47 | + workbook = openpyxl.Workbook() |
| 48 | + sheet = workbook.active |
| 49 | + sheet.title = sheet_name |
| 50 | + for i in range(0, index): |
| 51 | + id = value[i].get('id', '') |
| 52 | + title = value[i].get('title', '') |
| 53 | + shopRepurchaseRate = value[i].get('shopRepurchaseRate', '') |
| 54 | + tradeNum = value[i].get('tradeNum', '') |
| 55 | + url = value[i].get('url', '') |
| 56 | + cell = [id, title, shopRepurchaseRate, tradeNum, url] |
| 57 | + sheet.cell(row=1, column=1, value='ID') |
| 58 | + sheet.cell(row=1, column=2, value='标题') |
| 59 | + sheet.cell(row=1, column=3, value='回购率') |
| 60 | + sheet.cell(row=1, column=4, value='成交量') |
| 61 | + sheet.cell(row=1, column=5, value='链接') |
| 62 | + for j in range(0, len(cell)): |
| 63 | + sheet.cell(row=i+2, column=j+1, value=str(cell[j])) |
| 64 | + workbook.save(path) |
| 65 | + print("xlsx格式表格写入数据成功!") |
| 66 | + |
| 67 | + |
| 68 | +def main(keyword, page): |
| 69 | + offer_list = get_premium_offer_list(keyword, page) |
| 70 | + print(offer_list) |
| 71 | + write_excel_xlsx('./data.xlsx', '数据', offer_list) |
| 72 | + |
| 73 | +if __name__ == '__main__': |
| 74 | + main("数据线", 10) |
| 75 | + |
| 76 | + |
| 77 | + |
0 commit comments