闲鱼数据的获取与下载
25-04-08
slbcun
959℃
0
今天给大家分享闲鱼的数据获取和逆向,只需要在终端输入想搜索的物品名,再输入想下载内容的页码,
对应数据的csv文件便会自动命名好并创建在相应的文件夹内,以供大家观看和相应需求。
值得注意的是cookies 过很长一段时间会失效,到时候则需要大家手动替换这一栏啦,也不是很难。
from hashlib import md5 import time import requests import csv import os from pathlib import Path # 定义路径 os_path = Path.cwd() / '闲鱼' print(os_path) # 检查目录是否存在,如果不存在则创建 if not os_path.exists(): os_path.mkdir(parents=True, exist_ok=True) # parents=True 支持多级目录创建 cookies = { 'cna': 'o2VyIG74FTMCASShMKcIxy+k', 'cookie2': '124e5ba3daf492b75775ddeb501c4470', '_samesite_flag_': 'true', 't': 'af37bded5c848ba4245c9527b710787f', '_tb_token_': 'e33ff5759af39', 'mtop_partitioned_detect': '1', '_m_h5_tk': '43a8d19ad104ac584f86e23e273c6aa8_1743764419015', '_m_h5_tk_enc': '37d8a137028ac2a98bb85db99720098f', 'xlly_s': '1', } #cookies过一段时间会刷新,记得改成自己电脑的cookies, headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 Edg/134.0.0.0', # 'cookie': 'cna=o2VyIG74FTMCASShMKcIxy+k; cookie2=124e5ba3daf492b75775ddeb501c4470; xlly_s=1; _samesite_flag_=true; t=af37bded5c848ba4245c9527b710787f; _tb_token_=e33ff5759af39; mtop_partitioned_detect=1; _m_h5_tk=179c6ebaaf128f51c3115d032879d73e_1743696440707; _m_h5_tk_enc=e52128cad70c5425624fe37abe435555; tfstk=ggljBubsPijXYqIoonLzPgrFhmFsCcOF5Nat-VCVWSFAXG3L4mr4gFA_Xmqrgou4MlCsbVlqbfgG1P3tjEqwoLumo5V9YHR6Tq00ME3tRjwYw_UIyPI9U-IeCSJJYHReaKQ81ntE02oQCLag2rUTB5L5ePzg6rFO6UZ85yXODcF9PUaTSsCAMrERwy4T6lnT6Ug8qPMEwQay7ogXkrjvDARFBqZ561h7yhqKlw5Qub4jHk3xG1fMJrwbvqEWxbU2383gCfbG1o3xKc4KDiKUnVHSVrN6atFKPAnQrjdFczDqy2r-RwCKoSMIOSHXqTVqcbF-Mv_O64Nbm8U7Fit7vj0-t7ey9eg-ZmumNV796zoU2qcY1BL3GSEYNzcwqsEseAh4n5jvY-0tF0ab9gP5TkZm3O_7K1a7YUT5IOj2Atuy2k1qP-UuzM8WPiN8Hz47YUT5IO2Yrz8ePUsbw', } keyword=input("请输入搜索物品:") page=input("请输入搜索页码:") data = { 'data': f'{{"pageNumber":"{page}","keyword": "{keyword}","fromFilter":false,"rowsPerPage":30,"sortValue":"","sortField":"","customDistance":"","gps":"","propValueStr":{{}},"customGps":"","searchReqFromPage":"pcSearch","extraFilterValue":"{{}}","userPositionJson":"{{}}"}}' } time = str(round(time.time() * 1000)) params = { 'jsv': '2.7.2', 'appKey': '34839810', 't': time, 'v': '1.0', 'type': 'originaljson', 'accountSite': 'xianyu', 'dataType': 'json', 'timeout': '20000', 'api': 'mtop.taobao.idlemtopsearch.pc.search', 'sessionOption': 'AutoLoginOnly', 'spm_cnt': 'a21ybx.search.0.0', 'spm_pre': 'a21ybx.search.searchInput.0', } token =cookies['_m_h5_tk'].split('_')[0] print(token) appKey = "34839810" data_1 = f'{{"pageNumber":"{page}","keyword": "{keyword}","fromFilter":false,"rowsPerPage":30,"sortValue":"","sortField":"","customDistance":"","gps":"","propValueStr":{{}},"customGps":"","searchReqFromPage":"pcSearch","extraFilterValue":"{{}}","userPositionJson":"{{}}"}}' i = ( token + "&" + time + "&" + appKey + "&" + data_1) sign=md5(i.encode()).hexdigest() params['sign']=sign print(data,data_1) response = requests.post( 'https://h5api.m.goofish.com/h5/mtop.taobao.idlemtopsearch.pc.search/1.0/', params=params, cookies=cookies, headers=headers, data=data, ) result=response.json() print(result) resultlist=result['data']['resultList'] for index in resultlist: try: name = index['data']['item']['main']['exContent']['userNickName'] except: name = "未知" try: price = index['data']['item']['main']['exContent']['detailParams'][ 'soldPrice'] except: price = "未知" shop_id = index['data']['item']['main']['exContent']['detailParams'][ 'itemId'] shop_link = f'https://www.goofish.com/item?id=893424239322&categoryId={shop_id}' dit = {"用户名": name, "地区": index['data']['item']['main']['exContent']['area'], "售价": price, "标题": index['data']['item']['main']['exContent']['detailParams'][ 'title'], '详情页': shop_link} fieldnames = [ "用户名", "地区", "售价", "标题", '详情页'] # 打开文件并写入数据 import csv import os # 定义字段名 fieldnames = [ "用户名", "地区", "售价", "标题", "详情页" ] # 检查文件是否存在(决定是否需要写入表头) file_name = f"{keyword}.csv" file_path = os_path / file_name file_exists = os.path.isfile(f'["{file_name}.csv"]') # 打开文件并写入数据 with open(file_path , mode="a", newline="", encoding="utf-8") as file: csv_writer = csv.DictWriter(file, fieldnames=fieldnames) # 如果文件不存在或为空,则写入表头 if not file_exists: csv_writer.writeheader() # 写入一行数据 csv_writer.writerow(dit) print(dit)