1.打开淘宝,登录并且输入查询的物品,点击右键,点击检查
2.找到2.0/?jsv右击选择curl(bash)复制到在线curl命令转代码 选择python转换,得到代码粘贴起来,得到response。
3.复制到代码到python,运行response.text()得到网页的文本内容。
3.对复制过来的代码里的params参数进行处理,删除'callback': 'mtopjsonp1',并且在'type': 'json', 'dataType': 'json',不用'jsonp'是为了后续可以使用response.json(),不用txt()处理。注意引用json库。
4.读取json数据保存到csv文件中。
5.最终代码:
import requests
cookies = {
'miid': '141439373371660658',
't': 'aa8febcd45b715e1dccbe4f13510772a',
'cna': 'bGsBGuHwHmQCAXju+IJgl+YX',
'lgc': 'tb9330232098',
'tracknick': 'tb9330232098',
'thw': 'cn',
'ariaDefaultTheme': 'undefined',
'mtop_partitioned_detect': '1',
'_m_h5_tk': 'cc474cb1f65594c846e5fbcaff6c1f28_1708152365495',
'_m_h5_tk_enc': '50336096f5a095e861e7a38db51e4cd2',
'tk_trace': 'oTRxOWSBNwn9evfHtXQifmafmYzSU80EW6dedixskcJ%2Bc8J0R27pVpwR009bxUw3pJYMDEOjj9VTZncJdZFFzzCuQZlxUrfnSMxQAzTdsNfSmKKyqWcqKPn9v89RJSQ0oG%2FogMwgfvZEKclhcLXWhjzh3JBkQjA%2FL%2FOJkbI0oZ5jqzn2VG%2BKJci6PiuNfv4VwjmPxjt4W%2F83B9FUm3W%2FUoJxmXPRtc3s4xkDIrIG7gqxqJXEReV7jHT%2FgHjGUbuPSdePWpHou6KT1yXAyBnPwkfkI02byxjELdw9%2FCnKL7Iq4zadeWIERo47JW2ATJ2%2BGwPsEvJ48c%2Bk77bgKLvHB80SCgzUiEahg5BC9UQup1n9buUdjJ6NTEX8h6oM2W02ynYh5%2B3JyqGl0epQEXidvXp1hWDRmM5RtzB27%2FxcxSF84Q%3D%3D',
'cookie2': '1d92c1a5ff6fdef92705799a5af7735b',
'_tb_token_': '635e35bd9098',
'mt': 'ci=-1_0',
'_samesite_flag_': 'true',
'3PcFlag': '1708143394509',
'sgcookie': 'E100gH1TeVwBo1xKjL1pQrgJh%2Fw71O6qROkVBu0vqL5SWD%2BjBGqAkWHco8sWiWQQ7oaW%2BQQCV64b7dl3L8RWeoWhvZjGGN5IiG4%2Bw1ZXVqi51LkJSAY%2FZy4RpgVBtidp%2FJmF',
'unb': '2212187893946',
'uc1': 'existShop=false&cookie14=UoYenby%2B65DgNA%3D%3D&cookie15=URm48syIIVrSKA%3D%3D&cookie16=VT5L2FSpNgq6fDudInPRgavC%2BQ%3D%3D&pas=0&cookie21=W5iHLLyFe3xm',
'uc3': 'lg2=V32FPkk%2Fw0dUvg%3D%3D&id2=UUpgRKyMGLkMj4yM%2Bg%3D%3D&nk2=F5RMGoevINQpxZ1H&vt3=F8dD3eu2Nt1EHpHuHKw%3D',
'csg': '7c4e6fb4',
'cancelledSubSites': 'empty',
'cookie17': 'UUpgRKyMGLkMj4yM%2Bg%3D%3D',
'dnk': 'tb9330232098',
'skt': 'a559ddde015e7a2d',
'existShop': 'MTcwODE0MzQ0OQ%3D%3D',
'uc4': 'nk4=0%40FY4HX7UsnkJMHcfV%2FvxuM4Mhvok%2BNjE%3D&id4=0%40U2gqy133ULUMfb1HFmsUv%2FAJNYiWzPUf',
'_cc_': 'URm48syIZQ%3D%3D',
'_l_g_': 'Ug%3D%3D',
'sg': '86a',
'_nk_': 'tb9330232098',
'cookie1': 'BYS%2B9xq28PSJVkxozh5affcpHv1mWy0wrw92uFs5TBg%3D',
'isg': 'BI-P0-WpNlZZSzLWQSuT8mc6HiOZtOPWiUgXJaGcK_4FcK9yqYRzJo1mdqZOCLtO',
}
headers = {
'authority': 'h5api.m.taobao.com',
'accept': '*/*',
'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
# Requests sorts cookies= alphabetically
# 'cookie': 'miid=141439373371660658; t=aa8febcd45b715e1dccbe4f13510772a; cna=bGsBGuHwHmQCAXju+IJgl+YX; lgc=tb9330232098; tracknick=tb9330232098; thw=cn; ariaDefaultTheme=undefined; mtop_partitioned_detect=1; _m_h5_tk=cc474cb1f65594c846e5fbcaff6c1f28_1708152365495; _m_h5_tk_enc=50336096f5a095e861e7a38db51e4cd2; tk_trace=oTRxOWSBNwn9evfHtXQifmafmYzSU80EW6dedixskcJ%2Bc8J0R27pVpwR009bxUw3pJYMDEOjj9VTZncJdZFFzzCuQZlxUrfnSMxQAzTdsNfSmKKyqWcqKPn9v89RJSQ0oG%2FogMwgfvZEKclhcLXWhjzh3JBkQjA%2FL%2FOJkbI0oZ5jqzn2VG%2BKJci6PiuNfv4VwjmPxjt4W%2F83B9FUm3W%2FUoJxmXPRtc3s4xkDIrIG7gqxqJXEReV7jHT%2FgHjGUbuPSdePWpHou6KT1yXAyBnPwkfkI02byxjELdw9%2FCnKL7Iq4zadeWIERo47JW2ATJ2%2BGwPsEvJ48c%2Bk77bgKLvHB80SCgzUiEahg5BC9UQup1n9buUdjJ6NTEX8h6oM2W02ynYh5%2B3JyqGl0epQEXidvXp1hWDRmM5RtzB27%2FxcxSF84Q%3D%3D; cookie2=1d92c1a5ff6fdef92705799a5af7735b; _tb_token_=635e35bd9098; mt=ci=-1_0; _samesite_flag_=true; 3PcFlag=1708143394509; sgcookie=E100gH1TeVwBo1xKjL1pQrgJh%2Fw71O6qROkVBu0vqL5SWD%2BjBGqAkWHco8sWiWQQ7oaW%2BQQCV64b7dl3L8RWeoWhvZjGGN5IiG4%2Bw1ZXVqi51LkJSAY%2FZy4RpgVBtidp%2FJmF; unb=2212187893946; uc1=existShop=false&cookie14=UoYenby%2B65DgNA%3D%3D&cookie15=URm48syIIVrSKA%3D%3D&cookie16=VT5L2FSpNgq6fDudInPRgavC%2BQ%3D%3D&pas=0&cookie21=W5iHLLyFe3xm; uc3=lg2=V32FPkk%2Fw0dUvg%3D%3D&id2=UUpgRKyMGLkMj4yM%2Bg%3D%3D&nk2=F5RMGoevINQpxZ1H&vt3=F8dD3eu2Nt1EHpHuHKw%3D; csg=7c4e6fb4; cancelledSubSites=empty; cookie17=UUpgRKyMGLkMj4yM%2Bg%3D%3D; dnk=tb9330232098; skt=a559ddde015e7a2d; existShop=MTcwODE0MzQ0OQ%3D%3D; uc4=nk4=0%40FY4HX7UsnkJMHcfV%2FvxuM4Mhvok%2BNjE%3D&id4=0%40U2gqy133ULUMfb1HFmsUv%2FAJNYiWzPUf; _cc_=URm48syIZQ%3D%3D; _l_g_=Ug%3D%3D; sg=86a; _nk_=tb9330232098; cookie1=BYS%2B9xq28PSJVkxozh5affcpHv1mWy0wrw92uFs5TBg%3D; isg=BI-P0-WpNlZZSzLWQSuT8mc6HiOZtOPWiUgXJaGcK_4FcK9yqYRzJo1mdqZOCLtO',
'referer': 'https://s.taobao.com/',
'sec-ch-ua': '"Not A(Brand";v="99", "Microsoft Edge";v="121", "Chromium";v="121"',
'sec-ch-ua-mobile': '?1',
'sec-ch-ua-platform': '"Android"',
'sec-fetch-dest': 'script',
'sec-fetch-mode': 'no-cors',
'sec-fetch-site': 'same-site',
'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Mobile Safari/537.36 Edg/121.0.0.0',
}
params = {
'jsv': '2.6.2',
'appKey': '12574478',
't': '1708143731883',
'sign': '6cf1dfae2b568f66dfbde8e1a0297fc1',
'api': 'mtop.relationrecommend.WirelessRecommend.recommend',
'v': '2.0',
#改变 type 和 dataType
'type': 'json',
'dataType': 'json',
# 注释掉 'callback': 'mtopjsonp1',
'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\\":\\"1\\",\\"n\\":48,\\"q\\":\\"%E9%99%88%E7%9A%AE\\",\\"tab\\":\\"all\\",\\"pageSize\\":48,\\"totalPage\\":100,\\"totalResults\\":4800,\\"sourceS\\":\\"0\\",\\"sort\\":\\"_coefp\\",\\"bcoffset\\":\\"\\",\\"ntoffset\\":\\"\\",\\"filterTag\\":\\"\\",\\"service\\":\\"\\",\\"prop\\":\\"\\",\\"loc\\":\\"\\",\\"start_price\\":null,\\"end_price\\":null,\\"startPrice\\":null,\\"endPrice\\":null,\\"itemIds\\":null,\\"p4pIds\\":null}"}',
}
response = requests.get('https://h5api.m.taobao.com/h5/mtop.relationrecommend.wirelessrecommend.recommend/2.0/', params=params, cookies=cookies, headers=headers)
import csv
import json
json_data=response.json()
f=open("爬取淘宝数据.csv",mode='a',encoding='utf-8',newline='')
csv_write=csv.writer(f)
itemsArray=json_data['data']['itemsArray']
csv_write.writerow(['title','shopinfo','price','procity','realSales'])
for item in itemsArray:
title=item["title"]#商品内容
shopinfo=item['shopInfo']['title'] #店铺名称
price=item['price'] #价格
procity=item['procity']#发货地址
realSales=item['realSales'] #买的人数
# pricePerUnit=item['pricePerUnit']
print([title,shopinfo,price,procity,realSales])
csv_write.writerow([title,shopinfo,price,procity,realSales])
记得一定要完成前面几步去获取自己的cookie、header和params哦,不同的账号这些参数是不一样的。
点赞多的话下期写如何解决翻页问题。