#python #json #web-scraping #python-requests
Вопрос:
Я запускаю код json, они показывают мне «ошибка типа: индексы списка должны быть целыми числами или срезами, а не str». Я пытаюсь решить эту проблему, но ошибка не устраняется. Я, возможно, перепробовал все способы из Stackoverflow, чтобы решить эту проблему, но, к моему ужасу, ни один из них на самом деле не сработал. это мой файл Json, я буду благодарен, если вы его решите
import requests
headers = {
'authority': 'www.sephora.com',
'pragma': 'no-cache',
'cache-control': 'no-cache',
'sec-ch-ua': '^\^Google',
'sec-ch-ua-mobile': '?0',
'user-agent': 'Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36',
'sec-ch-ua-platform': '^\^Windows^\^',
'accept': '*/*',
'sec-fetch-site': 'same-origin',
'sec-fetch-mode': 'cors',
'sec-fetch-dest': 'empty',
'referer': 'https://www.sephora.com/search?keyword=shampoosamp;currentPage=1',
'accept-language': 'en-US,en;q=0.9',
'cookie': 'j9yt5Pdb=AEWd5bR6AQAAlIZUjBcGcMKlYTUS8Dl7mPnqAy_EKe1d0ZioXD2fVRLWcKuw^|1^|0^|b79cea881887aac8da99bbd896191d3c503c9b0d; ConstructorioID_client_id=05848fa7-09f7-420a-a4fb-eab35db0784f; site_language=en; site_locale=us; RES_TRACKINGID=27056549752191; ResonanceSegment=1; s_ecid=MCMID^%^7C70348848741041117474586787002397792966; ftr_ncd=6; _gcl_au=1.1.612636349.1626532629; _scid=9fb2d200-978a-4775-bfd7-948b5bdbe8bf; _fbp=fb.1.1626532634553.740075128; _pin_unauth=dWlkPVlURXhNRE0wWlRNdE4yTTROQzAwTm1aa0xXRmpZbUV0WWpVeE9HSTRNelprTnpFMw; ab.storage.deviceId.476615b3-3386-4e1c-a9fd-7e174eb9b8de=^%^7B^%^22g^%^22^%^3A^%^22d697aee4-c98b-240a-4ee4-0ea38c6efc16^%^22^%^2C^%^22c^%^22^%^3A1626691384817^%^2C^%^22l^%^22^%^3A1626691384817^%^7D; __olapicU=BsjoUhkYYVI98bXDDnaYpqqlgkbThrS2; _sctr=1^|1630911600000; _gid=GA1.2.888409794.1631095364; sti=c9de56ec-3ecd-4840-a4a5-16488e7cd86c; device_type=desktop; current_country=PK; check=true; forterToken=90ff331bafbf49c4b710dc8e3d2667c1_1631174313935__UDF43_9ck; rvi_33206422091=2390847; AMCVS_F6281253512D2BB50A490D45^%^40AdobeOrg=1; s_cc=true; bm_sz=D2294C478D580AB25E6267D830EC80A3~YAAQNZ4QAmu p h6AQAAie Oyg1VDDXe/znMBuKqSiOY5/CRRapkp3BHzo8kqmk6 RvtSlR2 Rr0guV336KGue/zrPEMrVQUMY44Yf7Z5KZRyLrxahf4Or8pjZT6YcsQHPYHvsHcuglczxTvCH7QgZEqdRRws6ZUxKVSDtkRG7k3fNqGsqH4qpaq/7WR66HCRh86hfe9AuzjXRDifYUGg5yrUtdWJi5PjZ0n5ZO8XeiT/7wlTIXaB1YiUGSgquSvxk3TQB3Aaeavw7/QfVbKPVYgfJvZfXPhjIUEX73gvEbzfl/D~3617333~3684665; _abck=2CE85441282C0E682A1D8C796E78E044~0~YAAQNZ4QAmq p h6AQAAie OygbTly8Tl2/Op64CRGsctQbceE5pyrJpt9rTao9XaTy8 I5fFIIBSSOSeOHI7Zc1wZvD16aKjUpkkzsuADdTVOy8gRR/UogA0mFemLqE7FgZftYKx9mc8XxjKy4DlBF CYh614ZPa5v9sKzz5l2vz6v1Hr1x6KtC9C7xMyWSamWpTz /D1yD96mpS9l5VCxn o8bKrA/R9ng7DL NX iRhlbsaHYf9t w7Tmxf27fu2s4UFuAAgs68XF6wnO2Egn0 vuSg5zRpyTTnrrke0prmV88FiNaeaxFLGI tuz3fdHxaXy9ivd13JydYYY4gDAmKK/t5xeE53B2ai1DtSyw6VAYvVReayS2YxKLvqXvmd6R6779Mrs 5RbvSdpNYziN7NEiB6grQ==~-1~-1~-1; akamweb=C; JSESSIONID=com2-106~u-cQUNiYnie0ndF1eItGc9nK.com2-106; ak_bmsc=98F7D7317300098F5DFC404F881EC00C~000000000000000000000000000000~YAAQNJ4QAlf4V4l7AQAAFj4iyw3aNipcKPXJE3tRr0jfsscB7dVnSXTRIUbi9V51j5t7E/YcGFF0fBSD1q2YRwws7rDxhki8/E61UrZ1t6dil0X0NziHmThRijUH31Sx166X rBYVFGk/cwc0bSDBWQ12gsDWrPDl5E7fDeeTFVNPrVEggb3CSpOaIMHV3g JlosS/5ZHVFC4 nG0g/aJIqrOAtfcUkJSmfvO4MqoydYAkxKEL6dI2AXvjifw24wZ7G18gq0J3vNDu4/kPiaphAQ082Ub/y6YU4dZKnvQpH0iSi6JSVdpB0F41ikHsiKmlikoK8EoiVyLFm wnaiCkAszg0nrm3Y6X4poUlD0GeQ1kml4UEKTKJjgn/xaMoCYvRyyxmKAyuksA==; mboxEdgeCluster=38; RES_SESSIONID=319638309600759; mp_sephora_mixpanel=^%^7B^%^22distinct_id^%^22^%^3A^%^20^%^2217ab4e63cb2e6-08c4dee76a42cf-3c3b590c-c0000-17ab4e63cb3254^%^22^%^2C^%^22bc_persist_updated^%^22^%^3A^%^201626532625876^%^2C^%^22g_search_engine^%^22^%^3A^%^20^%^22google^%^22^%^2C^%^22hasLovesList^%^22^%^3A^%^20false^%^7D; anaNextPageData=^%^7B^%^22pageName^%^22^%^3A^%^22search^%^3Aresults-products^%^3An^%^2Fa^%^3A*^%^22^%^2C^%^22pageType^%^22^%^3A^%^22search^%^22^%^7D; inside-sephora=581361997-b4683421d1ce3a863c50be65916024d59d569f008cc0c1a8659cbff48bac4655-0-0; s_visit=1; _uetsid=e89affd0108b11ec9da0795d1155978e; _uetvid=794a17c0e70c11eb8653ffab7e898cde; AMCV_F6281253512D2BB50A490D45^%^40AdobeOrg=-1712354808^%^7CMCIDTS^%^7C18879^%^7CMCMID^%^7C70348848741041117474586787002397792966^%^7CMCAAMLH-1631805461^%^7C3^%^7CMCAAMB-1631805461^%^7CRKhpRz8krg2tLO6pguXWp5olkAcUniQYPHaMWWgdJ3xzPWQmdj0y^%^7CMCOPTOUT-1631207861s^%^7CNONE^%^7CMCAID^%^7CNONE^%^7CvVersion^%^7C4.3.0; s_dl=1; _cs_mk=0.6812425561219304_1631200661401; _ga=GA1.2.740043533.1626532629; s_ppvl=search^%^253Aresults-products^%^253An^%^2Fa^%^253A^%^2A^%^2C10^%^2C10^%^2C645^%^2C1024^%^2C445^%^2C1024^%^2C768^%^2C1^%^2CL; _derived_epik=dj0yJnU9ZGpKdVRvRkxrb3JEZlQ1Sk04NzJSNGl0UXFoUm5OQjgmbj1kRnZlY05BMXJSRzV2c1dkQmFnbkdBJm09MSZ0PUFBQUFBR0U2SlpjJnJtPTEmcnQ9QUFBQUFHRTZKWmM; ab.storage.sessionId.476615b3-3386-4e1c-a9fd-7e174eb9b8de=^%^7B^%^22g^%^22^%^3A^%^222778330f-ae3f-b195-1852-e3222c269934^%^22^%^2C^%^22e^%^22^%^3A1631202471245^%^2C^%^22c^%^22^%^3A1631200671151^%^2C^%^22l^%^22^%^3A1631200671245^%^7D; _gat_gtag_UA_165841114_1=1; s_ppv=https^%^253A^%^2F^%^2Fwww.sephora.com^%^2Fsearch^%^253Fkeyword^%^253Dshampoos^%^2526currentPage^%^253D1^%^2C10^%^2C10^%^2C645^%^2C1024^%^2C221^%^2C1024^%^2C768^%^2C1^%^2CL; AKA_A2=A; _ga_HX1JZMWBCJ=GS1.1.1631200656.24.0.1631200711.5; bm_sv=FD443C099E3DCDEA460EAF234F5913A9~aorTT JhfdS34e5IPOi9ZLp9GpZEiuETFe2Q7KQf16exRX6edCAkCHdYkOnb VlGUab eMvq9OdaSWEv8mztQaDlLiDfgrX1JR6d42BiwX8sf5YNLQJI34D7X9yYwnOIu0564oESZA69QWwFee/ccQtak9wHuYgXPkBk62k0BIg=; akavpau_akaau=1631201311~id=66e68a40ff585e5badeef866401b6c16; RT=^\^z=1amp;dm=www.sephora.comamp;si=9825b493-1511-434d-9d0e-1be93c7da1bcamp;ss=ktcn2g8damp;sl=ramp;tt=700eamp;bcn=^%^2F^%^2F684d0d3e.akstat.io^%^2F^\^; mbox=PC^#122cf4fbefba433abe7d6820cfb0ea97.38_0^#1694445515^|session^#eab0a9135ea0455686d59c0c0741ce9f^#1631202486',
}
params = (
('type', 'keyword'),
('q', 'shampoos'),
('currentPage', ['1', '1']),
('content', 'true'),
('includeRegionsMap', 'true'),
('page', '60'),
('constructorSessionID', '23'),
('constructorClientID', '05848fa7-09f7-420a-a4fb-eab35db0784f'),
('targetSearchEngine', 'nlp'),
)
r = requests.get('https://www.sephora.com/api/catalog/search', headers=headers, params=params)
#NB. Original query string below. It seems impossible to parse and
#reproduce query strings 100% accurately so the one below is given
#in case the reproduced version is not "correct".
# response = requests.get('https://www.sephora.com/api/catalog/search?type=keywordamp;q=shampoosamp;currentPage=1amp;content=trueamp;includeRegionsMap=trueamp;page=60amp;currentPage=1amp;constructorSessionID=23amp;constructorClientID=05848fa7-09f7-420a-a4fb-eab35db0784famp;targetSearchEngine=nlp', headers=headers)
data=r.json()
print(len(data['categories']['categories']['products']))
Ответ №1:
Удалите ['categories']['categories']
:
print(len(data["products"]))
С принтами:
60