一、爬取目标
二、完整源码
#""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
#
# Copyright (c) 2024 愤怒的it男, All Rights Reserved.
# FileName : code.py
# Date : 2024.01.20
# Author : 愤怒的it男
# Version : 1.0.0
# Node : 欢迎关注微信公众号【愤怒的it男】
#
#""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
import csv
import json
import requests
from tabulate import tabulate
def getData(baseUrl, headers):
response = requests.get(url=baseUrl, headers=headers)
pc_list = json.loads(response.text[6:-2])['data']['pc_list']
data = []
for pc in pc_list:
link = 'https://www.mi.com/shop/buy/detail?product_id={}'.format(pc['product_id'])
for commodity in pc['commodity_list']:
commodity['desc'] = commodity['desc'].replace("<font color='#ff4a00'>", "").replace("</font>", "-").strip()
data.append([commodity['name'],commodity['price'],link,commodity['desc']])
return data
def printData(result):
headers = ["名称", "价格", "购买链接", "简介描述"]
table = tabulate(result, headers=headers, maxcolwidths=[None, None, None, 50], tablefmt='grid')
print(table)
def saveData(result):
with open('小米手机.csv', 'w', encoding='utf-8', newline='') as file:
writer = csv.writer(file)
writer.writerows(result)
def main():
baseUrls = ['https://api2.order.mi.com/search/index?query=手机&page_index=1&page_size=20&filter_tag=0&main_sort=0&province_id=2&city_id=36&callback=__jp0',
'https://api2.order.mi.com/search/index?query=手机&page_index=2&page_size=20&filter_tag=0&main_sort=0&province_id=2&city_id=36&callback=__jp5',
'https://api2.order.mi.com/search/index?query=手机&page_index=3&page_size=20&filter_tag=0&main_sort=0&province_id=2&city_id=36&callback=__jp6'
]
headers = {'Referer': 'https://www.mi.com/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
result = []
for baseUrl in baseUrls:
data = getData(baseUrl, headers)
result = result+data
printData(result)
saveData(result)
if __name__== "__main__" :
main()
三、源码简析
1、请求头Referer
在Chrome浏览器开发者工具中抓包分析,可以知道本实例请求的引荐来源网址政策为strict-origin-when-cross-origin,即只允许同源网站可以请求该数据,因此请求头的Referer需要设置为https://www.mi.com/
headers = {'Referer': 'https://www.mi.com/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
2、tabulate库
tabulate和prettytable是python的表格美观输出的两大名库。本来是用使用prettytable库来输出美观表格的,编码过程中发现“简介描述”字段文字太长,换行导致输出错乱,而prettytable又没有自动换行的功能,所以后来安装了tabulate库。tabulate库只需设置一下maxcolwidths参数就可以方便输出长文字了,完美!
from tabulate import tabulate
def printData(result):
headers = ["名称", "价格", "购买链接", "简介描述"]
table = tabulate(result, headers=headers, maxcolwidths=[None, None, None, 50], tablefmt='grid')
print(table)
四、爬取结果
+-------------------------------------------+--------+--------------------------------------------------------+----------------------------------------------------+
| 名称 | 价格 | 购买链接 | 简介描述 |
+===========================================+========+========================================================+====================================================+
| Xiaomi 14 12GB+256GB | 4299 | https://www.mi.com/shop/buy/detail?product_id=19300 | 「限定非凡礼盒热卖中,限量定制色全新上市,小米汽车 |
| | | | 同款配色!购机至高享24期分期免息」-徕卡光学Summilu |
| | | | x镜头|光影猎人900影像传感器 | 75mm徕卡浮动长焦 | |
| | | | 小米澎湃OS |第三代骁龙®8移动平台|小米环形冷泵 |
+-------------------------------------------+--------+--------------------------------------------------------+----------------------------------------------------+
| Xiaomi 14 8GB+256GB | 3999 | https://www.mi.com/shop/buy/detail?product_id=19300 | 「限定非凡礼盒热卖中,限量定制色全新上市,小米汽车 |
| | | | 同款配色!购机至高享24期分期免息」-徕卡光学Summilu |
| | | | x镜头|光影猎人900影像传感器 | 75mm徕卡浮动长焦 | |
| | | | 小米澎湃OS |第三代骁龙®8移动平台|小米环形冷泵 |
+-------------------------------------------+--------+--------------------------------------------------------+----------------------------------------------------+
| Xiaomi 14 16GB+512GB | 4599 | https://www.mi.com/shop/buy/detail?product_id=19300 | 「限定非凡礼盒热卖中,限量定制色全新上市,小米汽车 |
| | | | 同款配色!购机至高享24期分期免息」-徕卡光学Summilu |
| | | | x镜头|光影猎人900影像传感器 | 75mm徕卡浮动长焦 | |
| | | | 小米澎湃OS |第三代骁龙®8移动平台|小米环形冷泵 |
+-------------------------------------------+--------+--------------------------------------------------------+----------------------------------------------------+
| Xiaomi 14 16GB+1TB | 4999 | https://www.mi.com/shop/buy/detail?product_id=19300 | 「限定非凡礼盒热卖中,限量定制色全新上市,小米汽车 |
| | | | 同款配色!购机至高享24期分期免息」-徕卡光学Summilu |
| | | | x镜头|光影猎人900影像传感器 | 75mm徕卡浮动长焦 | |
| | | | 小米澎湃OS |第三代骁龙®8移动平台|小米环形冷泵 |
+-------------------------------------------+--------+--------------------------------------------------------+----------------------------------------------------+
| Xiaomi 14 限量定制版 | 4999 | https://www.mi.com/shop/buy/detail?product_id=19300 | 「限定非凡礼盒热卖中,限量定制色全新上市,小米汽车 |
| | | | 同款配色!购机至高享24期分期免息」-徕卡光学Summilu |
| | | | x镜头|光影猎人900影像传感器 | 75mm徕卡浮动长焦 | |
| | | | 小米澎湃OS |第三代骁龙®8移动平台|小米环形冷泵 |
+-------------------------------------------+--------+--------------------------------------------------------+----------------------------------------------------+
| Redmi K70 12GB+256GB | 2499 | https://www.mi.com/shop/buy/detail?product_id=19432 | 「购机赠Redmi 手环2,小米商城限定热爱礼盒热销中, |
| | | | 购机至高享6期免息」-第二代骁龙8|小米澎湃OS|狂暴 |
| | | | 引擎3.0|冰封散热系统|2K 护眼屏|5000mAh超大电量 |
+-------------------------------------------+--------+--------------------------------------------------------+----------------------------------------------------+
| Redmi K70 16GB+256GB | 2699 | https://www.mi.com/shop/buy/detail?product_id=19432 | 「购机赠Redmi 手环2,小米商城限定热爱礼盒热销中, |
| | | | 购机至高享6期免息」-第二代骁龙8|小米澎湃OS|狂暴 |
| | | | 引擎3.0|冰封散热系统|2K 护眼屏|5000mAh超大电量 |
+-------------------------------------------+--------+--------------------------------------------------------+----------------------------------------------------+
| Redmi K70 16GB+512GB | 2999 | https://www.mi.com/shop/buy/detail?product_id=19432 | 「购机赠Redmi 手环2,小米商城限定热爱礼盒热销中, |
| | | | 购机至高享6期免息」-第二代骁龙8|小米澎湃OS|狂暴 |
| | | | 引擎3.0|冰封散热系统|2K 护眼屏|5000mAh超大电量 |
+-------------------------------------------+--------+--------------------------------------------------------+----------------------------------------------------+
| Redmi K70 16GB+1TB | 3399 | https://www.mi.com/shop/buy/detail?product_id=19432 | 「购机赠Redmi 手环2,小米商城限定热爱礼盒热销中, |
| | | | 购机至高享6期免息」-第二代骁龙8|小米澎湃OS|狂暴 |
| | | | 引擎3.0|冰封散热系统|2K 护眼屏|5000mAh超大电量 |
+-------------------------------------------+--------+--------------------------------------------------------+----------------------------------------------------+
五、免责声明
本公众号所有源码均为个人学习所编写,仅可用于计算机技术学习及研究等合法行为,禁止利用本公众号的源码从事任何违反本国(地区)法律法规的业务,如有发现存在违法违规行为我会举报到网监部门。
原文始发于微信公众号(愤怒的it男):Python爬虫|小米手机官网所有机型
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/234310.html