Python practical case – obtaining Tao product data

Hi~ Hello everyone, this is the Demon King? ~!

For more python source code/information/answers/tutorials, etc. Click here to jump to the end of the article and get the business card for free.

Development environment:

  • python 3.8

  • pycharm professional version

Third-party libraries:

  • requests >>> pip install requests

Module installation:

win + R, enter cmd and enter the installation command pip install module name (if you think the installation speed is slow, you can switch the domestic mirror source)

Detailed implementation process

1. First find the data source

https://h5api.m.taobao.com/h5/mtop.relationrecommend.wirelessrecommend.recommend/2.0/

2. Code implementation

  1. Send a request (use code to access the corresponding URL)

  2. retrieve data

  3. Parse the data (extract data, extract what we need and discard what is not needed)

  4. Save data (table file)

Code display

'''
Have a question and no one has an answer? The editor has created a Python learning and communication QQ group: 926207505
Looking for like-minded friends to help each other, there are also good video learning tutorials and PDF e-books in the group!
'''
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; xHF; unb=2210627905944; uc3=vt3 =F8dCsGrJbnJiU/9Ga8E= & amp;lg2=WqG3DMC9VAQiUQ== & amp;id2=UUpgRsItw+rsB7dvyw== & amp;nk2=F5RDKmf768KMcHQ=; csg=8cbb3b15; lgc=tb668512329; canceledSubSites=empty; cookie 17=UUpgRsItw+rsB7dvyw== ; dnk=tb668512329; skt=9fdf261563c694c8; existShop=MTY5NjU5NjQ2Nw==; snekww==; tracknick=tb668512329; _cc_=UIHiLt3xSw==; _l_g_=Ug==; sg _m_h5_tk _enc=0c7861c6782983da6e928eee2638bab8; mt=ci=5_1; thw=cn; uc1=cookie16=W5iHLLyFPlMGbLDwA+dvAGZqLg== & amp;pas=0 & amp; cookie15 = uihilt3xd8xytw == & amp; cookie14 = uoe9a75qq35xwsg == & amp; existShop = true & amp; cookie21 = Urm48Syizjfmz9wv9wv Ctpzeq ==; x5Sec = 7B22617365727665723B32223A22313633331313131316664666346396466373465624B63674B 6B47454969726E4E4C392F2F2F2F2F2F2F2F2F2F77456147A49794D5441324D6A63354D4451374D7A43437532F2F2F2F2F2F2F2F2F2F2F2F2F2F2F2F2F2F2F 33B32223A22623336234343634616393232376227d; TFSTK = DINP3QW39CAN6ZTuealMamyufopMSDHETXKFQ7VHPFHTF5JKLKYZ2BFTAW43AWJ82YHRZBB Hyl8nsnbfo-FXLVRZI0JMNDE7DJIEAYC9VZFDUODUODINBGCYR3V2A-E_EDFJXD02BR7N7WC_Q_JULWGVGC6ZHMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMML pk; L = FBRY7YTILF1CVZ6JBO5AOURZA777777777777777777777777777777777777777777777777777777777777777ofir8SPZIIEG 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 & amp;appKey=12574478 & amp;t=1696596530127 & amp;sign =98d718024af85360a694c244e5a3324f & amp;api=mtop.relationrecommend.WirelessRecommend.recommend & amp;v=2.0 & amp;type=jsonp & amp;dataType=jsonp & amp;callback=mtopjsonp2 & amp;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}"}'
# 1. Send request
response = requests.get('https://h5api.m.taobao.com/h5/mtop.relationrecommend.wirelessrecommend.recommend/2.0/', params=params, headers=headers)
# 2. Get data
json_data = response.json() # The data taken out is in dictionary format
print(json_data)
# 3. Parse data
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])

Epilogue

Finally, thank you for reading my article~ This flight ends here

I hope this article has been helpful to you and learned some knowledge~

The hidden stars are also working hard to shine, and you should work hard too (let’s work hard together).

Finally, let’s spread the word~For more source codes, information, materials, answers, and exchanges click on the business card below to get it