嗨喽~大家好呀,这里是魔王呐 ❤ ~!
python更多源码/资料/解答/教程等 点击此处跳转文末名片免费获取
开发环境:
-
python 3.8
-
pycharm 专业版
第三方库:
- requests >>> pip install requests
模块安装:
win + R 输入cmd 输入安装命令 pip install 模块名 (如果你觉得安装速度比较慢, 你可以切换国内镜像源)
具体实现流程
一. 先找到 数据来源
https://h5api.m.taobao.com/h5/mtop.relationrecommend.wirelessrecommend.recommend/2.0/
二. 代码实现
-
发送请求 (用代码 访问对应的网址)
-
获取数据
-
解析数据 (提取数据 将我们需要的内容提取出来 不需要的 丢弃)
-
保存数据 (表格文件)
代码展示
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:926207505
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import requests
import csv
with open('taobao.csv', mode='a', newline='', encoding='utf-8') as f:
csv_writer = csv.writer(f)
csv_writer.writerow(['title', 'priceWap', 'realSales', 'procity', 'nick', 'shop_title', 'auctionURL'])
headers = {
'authority': 'h5api.m.taobao.com',
'accept': '*/*',
'accept-language': 'zh-CN,zh;q=0.9',
'cache-control': 'no-cache',
# Requests sorts cookies= alphabetically
'cookie': '_samesite_flag_=true; cookie2=17fdc5f669116c8d75f67ce944c6ba17; t=667fdce39eab056ef7b19da8e7fe353b; _tb_token_=5689eeae7e3ef; cna=mu+mHRxCj1QCAa8N4mhCCr/s; xlly_s=1; sgcookie=E1007RsuvyYcofrTp60eUzEeYuREh%2BLhQATs2YCXeu1Q5ztqk2sW%2F2s7DC1TzZr9stysDMnTMUMzkdb2VQy%2F7Et983chHDgBirypw5pe3Hnj7X89AeSKy6Wo3QMAhrfx0xHF; unb=2210627905944; uc3=vt3=F8dCsGrJbnJiU%2F9Ga8E%3D&lg2=WqG3DMC9VAQiUQ%3D%3D&id2=UUpgRsItw%2BrsB7dvyw%3D%3D&nk2=F5RDKmf768KMcHQ%3D; csg=8cbb3b15; lgc=tb668512329; cancelledSubSites=empty; cookie17=UUpgRsItw%2BrsB7dvyw%3D%3D; dnk=tb668512329; skt=9fdf261563c694c8; existShop=MTY5NjU5NjQ2Nw%3D%3D; uc4=id4=0%40U2gqyZJ81Yv14cp6ZGKPzfskvtMPt7HF&nk4=0%40FY4I6earzOZXUhcMjuCRA6YLsnekww%3D%3D; tracknick=tb668512329; _cc_=UIHiLt3xSw%3D%3D; _l_g_=Ug%3D%3D; sg=94f; _nk_=tb668512329; cookie1=WvY2bcMyBjwC2%2FESfKPhqaOXs%2FXPxaxugpcVR2PVSmM%3D; _m_h5_tk=ab3493f51223f8cdd2215bc651ccfaeb_1696603673580; _m_h5_tk_enc=0c7861c6782983da6e928eee2638bab8; mt=ci=5_1; thw=cn; uc1=cookie16=W5iHLLyFPlMGbLDwA%2BdvAGZqLg%3D%3D&pas=0&cookie15=UIHiLt3xD8xYTw%3D%3D&cookie14=Uoe9a75Q35xwSg%3D%3D&existShop=true&cookie21=URm48syIZJfmZ9wVCtpzEQ%3D%3D; x5sec=7b22617365727665723b32223a223136336335633136323131666439646634656339646466306237393465636562434f4b63674b6b47454969726a4e4c392f2f2f2f2f774561447a49794d5441324d6a63354d4455354e4451374d7a4343747532332f502f2f2f2f384251414d3d222c22733b32223a2262336234313436346163393239323762227d; tfstk=dinp3Qw39CAn6ZtUeaLMaMyUFopMsDHetXkfq7VhPfhtF5jkLkYz2bFtaW43AWj82YhrZbHyL8NSNbFo-FxmLvrzVI0JmnDE7djieAYc9vZFduODinbGCyR3V2A-eLS_E1_edFjxD02Br7n7Wc_q_JULwgVO2fo1myNjb5sWGc6Zhmmve310M8bBBdQPzywwJ3ppk; l=fBrY7YtILf1CVZ6jBO5aourza77OFIRb8sPzaNbMiIEGa6tATFpWJNCtDD-M7dtjgTfmGetyCykutd3XrX438x9Zu6iXRs5mpRp6-bpU-L5..; isg=BOvruKieChJnIlEvHw2wpudZeg_VAP-C3bPHGl1odSqN_Ape5dXB0-AaVjySXFd6',
'pragma': 'no-cache',
'referer': 'https://s.taobao.com/',
'sec-ch-ua': '"Google Chrome";v="117", "Not;A=Brand";v="8", "Chromium";v="117"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'script',
'sec-fetch-mode': 'no-cors',
'sec-fetch-site': 'same-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36',
}
params = {
'jsv': '2.6.2',
'appKey': '12574478',
't': '1696596530127',
'sign': '98d718024af85360a694c244e5a3324f',
'api': 'mtop.relationrecommend.WirelessRecommend.recommend',
'v': '2.0',
'type': 'json',
'dataType': 'json',
'data': '{"appId":"34385","params":"{\\"device\\":\\"HMA-AL00\\",\\"isBeta\\":\\"false\\",\\"grayHair\\":\\"false\\",\\"from\\":\\"nt_history\\",\\"brand\\":\\"HUAWEI\\",\\"info\\":\\"wifi\\",\\"index\\":\\"4\\",\\"rainbow\\":\\"\\",\\"schemaType\\":\\"auction\\",\\"elderHome\\":\\"false\\",\\"isEnterSrpSearch\\":\\"true\\",\\"newSearch\\":\\"false\\",\\"network\\":\\"wifi\\",\\"subtype\\":\\"\\",\\"hasPreposeFilter\\":\\"false\\",\\"prepositionVersion\\":\\"v2\\",\\"client_os\\":\\"Android\\",\\"gpsEnabled\\":\\"false\\",\\"searchDoorFrom\\":\\"srp\\",\\"debug_rerankNewOpenCard\\":\\"false\\",\\"homePageVersion\\":\\"v7\\",\\"searchElderHomeOpen\\":\\"false\\",\\"search_action\\":\\"initiative\\",\\"sugg\\":\\"_4_1\\",\\"sversion\\":\\"13.6\\",\\"style\\":\\"list\\",\\"ttid\\":\\"600000@taobao_pc_10.7.0\\",\\"needTabs\\":\\"true\\",\\"areaCode\\":\\"CN\\",\\"vm\\":\\"nw\\",\\"countryNum\\":\\"156\\",\\"m\\":\\"pc\\",\\"page\\":2,\\"n\\":48,\\"q\\":\\"iPhone\\",\\"tab\\":\\"all\\",\\"pageSize\\":\\"48\\",\\"totalPage\\":\\"100\\",\\"totalResults\\":\\"20250\\",\\"sourceS\\":\\"0\\",\\"sort\\":\\"_coefp\\",\\"bcoffset\\":\\"-3\\",\\"ntoffset\\":\\"3\\",\\"filterTag\\":\\"\\",\\"service\\":\\"\\",\\"prop\\":\\"\\",\\"loc\\":\\"\\",\\"start_price\\":null,\\"end_price\\":null,\\"startPrice\\":null,\\"endPrice\\":null}"}',
}
# url='https://h5api.m.taobao.com/h5/mtop.relationrecommend.wirelessrecommend.recommend/2.0/?jsv=2.6.2&appKey=12574478&t=1696596530127&sign=98d718024af85360a694c244e5a3324f&api=mtop.relationrecommend.WirelessRecommend.recommend&v=2.0&type=jsonp&dataType=jsonp&callback=mtopjsonp2&data=%7B%22appId%22%3A%2234385%22%2C%22params%22%3A%22%7B%5C%22device%5C%22%3A%5C%22HMA-AL00%5C%22%2C%5C%22isBeta%5C%22%3A%5C%22false%5C%22%2C%5C%22grayHair%5C%22%3A%5C%22false%5C%22%2C%5C%22from%5C%22%3A%5C%22nt_history%5C%22%2C%5C%22brand%5C%22%3A%5C%22HUAWEI%5C%22%2C%5C%22info%5C%22%3A%5C%22wifi%5C%22%2C%5C%22index%5C%22%3A%5C%224%5C%22%2C%5C%22rainbow%5C%22%3A%5C%22%5C%22%2C%5C%22schemaType%5C%22%3A%5C%22auction%5C%22%2C%5C%22elderHome%5C%22%3A%5C%22false%5C%22%2C%5C%22isEnterSrpSearch%5C%22%3A%5C%22true%5C%22%2C%5C%22newSearch%5C%22%3A%5C%22false%5C%22%2C%5C%22network%5C%22%3A%5C%22wifi%5C%22%2C%5C%22subtype%5C%22%3A%5C%22%5C%22%2C%5C%22hasPreposeFilter%5C%22%3A%5C%22false%5C%22%2C%5C%22prepositionVersion%5C%22%3A%5C%22v2%5C%22%2C%5C%22client_os%5C%22%3A%5C%22Android%5C%22%2C%5C%22gpsEnabled%5C%22%3A%5C%22false%5C%22%2C%5C%22searchDoorFrom%5C%22%3A%5C%22srp%5C%22%2C%5C%22debug_rerankNewOpenCard%5C%22%3A%5C%22false%5C%22%2C%5C%22homePageVersion%5C%22%3A%5C%22v7%5C%22%2C%5C%22searchElderHomeOpen%5C%22%3A%5C%22false%5C%22%2C%5C%22search_action%5C%22%3A%5C%22initiative%5C%22%2C%5C%22sugg%5C%22%3A%5C%22_4_1%5C%22%2C%5C%22sversion%5C%22%3A%5C%2213.6%5C%22%2C%5C%22style%5C%22%3A%5C%22list%5C%22%2C%5C%22ttid%5C%22%3A%5C%22600000%40taobao_pc_10.7.0%5C%22%2C%5C%22needTabs%5C%22%3A%5C%22true%5C%22%2C%5C%22areaCode%5C%22%3A%5C%22CN%5C%22%2C%5C%22vm%5C%22%3A%5C%22nw%5C%22%2C%5C%22countryNum%5C%22%3A%5C%22156%5C%22%2C%5C%22m%5C%22%3A%5C%22pc%5C%22%2C%5C%22page%5C%22%3A2%2C%5C%22n%5C%22%3A48%2C%5C%22q%5C%22%3A%5C%22iPhone%5C%22%2C%5C%22tab%5C%22%3A%5C%22all%5C%22%2C%5C%22pageSize%5C%22%3A%5C%2248%5C%22%2C%5C%22totalPage%5C%22%3A%5C%22100%5C%22%2C%5C%22totalResults%5C%22%3A%5C%2220250%5C%22%2C%5C%22sourceS%5C%22%3A%5C%220%5C%22%2C%5C%22sort%5C%22%3A%5C%22_coefp%5C%22%2C%5C%22bcoffset%5C%22%3A%5C%22-3%5C%22%2C%5C%22ntoffset%5C%22%3A%5C%223%5C%22%2C%5C%22filterTag%5C%22%3A%5C%22%5C%22%2C%5C%22service%5C%22%3A%5C%22%5C%22%2C%5C%22prop%5C%22%3A%5C%22%5C%22%2C%5C%22loc%5C%22%3A%5C%22%5C%22%2C%5C%22start_price%5C%22%3Anull%2C%5C%22end_price%5C%22%3Anull%2C%5C%22startPrice%5C%22%3Anull%2C%5C%22endPrice%5C%22%3Anull%7D%22%7D'
# 1. 发送请求
response = requests.get('https://h5api.m.taobao.com/h5/mtop.relationrecommend.wirelessrecommend.recommend/2.0/', params=params, headers=headers)
# 2. 获取数据
json_data = response.json() # 取出来的数据就是字典格式
print(json_data)
# 3. 解析数据
itemsArray = json_data.get('data').get('itemsArray')
for item in itemsArray:
title = item.get("title")
priceWap = item.get("priceWap")
realSales = item.get("realSales")
procity = item.get("procity")
nick = item.get("nick")
shop_title = item.get("shopInfo").get("title")
auctionURL = item.get("auctionURL")
print(title, priceWap, realSales, procity, nick, shop_title, auctionURL)
with open('taobao.csv', mode='a', newline='', encoding='utf-8') as f:
csv_writer = csv.writer(f)
csv_writer.writerow([title, priceWap, realSales, procity, nick, shop_title, auctionURL])
尾语
最后感谢你观看我的文章呐~本次航班到这里就结束啦 🛬
希望本篇文章有对你带来帮助 🎉,有学习到一点知识~
躲起来的星星🍥也在努力发光,你也要努力加油(让我们一起努力叭)。