0
0 комментариев

Добрый день спарсить данные json на python?

сам json

/jsonp/s7ViewResponse({«set»:
{«pv»:»1.0″,»type»:»img_set»,»n»:»EBFL2/65031208″,»item»:[{«i»:{«n»:»EBFL2/65031208_fr_sc7″},»s»:{«n»:»EBFL2/65031208_fr_sc7″},»dx»:»2000″,»dy»:»1728″,»iv»:»bAlQr0″},{«i»:{«n»:»EBFL2/65031208_a1_sc7″},»s»:{«n»:»EBFL2/65031208_a1_sc7″},»dx»:»2000″,»dy»:»1790″,»iv»:»n2PQj0″},{«i»:{«n»:»EBFL2/65031208_a2_sc7″},»s»:{«n»:»EBFL2/65031208_a2_sc7″},»dx»:»1113″,»dy»:»2000″,»iv»:»_GWPF1″},{«i»:{«n»:»EBFL2/65031208_a3_sc7″},»s»:{«n»:»EBFL2/65031208_a3_sc7″},»dx»:»767″,»dy»:»2000″,»iv»:»3IbRB0″},{«i»:{«n»:»EBFL2/65031208_bo_sc7″},»s»:{«n»:»EBFL2/65031208_bo_sc7″},»dx»:»2000″,»dy»:»784″,»iv»:»hvpQB0″}]}},»»);

на выходе должно получиться это
65031208_fr_sc7
EBFL2/65031208_fr_sc7
EBFL2/65031208_a1_sc7

мой код

gh = requests.get(protimg).text
gurl = gj.json_string = {"n": ''}
ljos = json.loads(gurl)
print(ljos)

выходит ошибка:

    Traceback (most recent call last):
  File "/opt/project/final-score.py", line 115, in <module>
    main()
  File "/opt/project/final-score.py", line 111, in main
    final_score()
  File "/opt/project/final-score.py", line 102, in final_score
    gurl = gh.json_string = {"n": ''}
AttributeError: 'str' object has no attribute 'json_string

update

»’
коментарии
urlimg = ‘http://images.footlocker.com/is/image/EBFL2/
prodictit = id продукта оно разное
»’

imggurl = urljoin(urlimg, productid)
protimg = '{url{ext}'.format(url=imggurl,ext='req=set,json&handler=s7ViewResponse&')

вывод

http://images.footlocker.com/is/image/EBFL2/65031208?req=set,json&handler=s7ViewResponse&
http://images.footlocker.com/is/image/EBFL2/6634A?req=set,json&handler=s7ViewResponse&
http://images.footlocker.com/is/image/EBFL2/A1GJB544?req=set,json&handler=s7ViewResponse&
http://images.footlocker.com/is/image/EBFL2/65031208?req=set,json&handler=s7ViewResponse&
http://images.footlocker.com/is/image/EBFL2/153662C?req=set,json&handler=s7ViewResponse&
http://images.footlocker.com/is/image/EBFL2/57937300?req=set,json&handler=s7ViewResponse&
http://images.footlocker.com/is/image/EBFL2/54554001?req=set,json&handler=s7ViewResponse&
http://images.footlocker.com/is/image/EBFL2/BM1000BK?req=set,json&handler=s7ViewResponse&
http://images.footlocker.com/is/image/EBFL2/6K1L0101?req=set,json&handler=s7ViewResponse&
http://images.footlocker.com/is/image/EBFL2/S79742?req=set,json&handler=s7ViewResponse&
http://images.footlocker.com/is/image/EBFL2/36182203?req=set,json&handler=s7ViewResponse&
http://images.footlocker.com/is/image/EBFL2/96611861?req=set,json&handler=s7ViewResponse&
http://images.footlocker.com/is/image/EBFL2/10800724?req=set,json&handler=s7ViewResponse&
http://images.footlocker.com/is/image/EBFL2/05899101?req=set,json&handler=s7ViewResponse&
http://images.footlocker.com/is/image/EBFL2/36336601?req=set,json&handler=s7ViewResponse&
http://images.footlocker.com/is/image/EBFL2/44018003?req=set,json&handler=s7ViewResponse&
http://images.footlocker.com/is/image/EBFL2/HFLPXBL?req=set,json&handler=s7ViewResponse&
http://images.footlocker.com/is/image/EBFL2/A3496LQT?req=set,json&handler=s7ViewResponse&
http://images.footlocker.com/is/image/EBFL2/AQ5518?req=set,json&handler=s7ViewResponse&
...............

продолжение кода

qr = requests.get(protimg)
j_strings = re.findall(r'({.*})', r.text)
 
 d = {}
    if j_strings:
    d = json.loads(j_strings[0])
 
 
    for k in d['set']['item']:
        dd = '{url}{ext}{end}'.format(url=imgurg, ext=k['i']['n'], end='?id=Y6YPH3?&wid=1280&hei=877&fmt.png')
        t = requests.get(dd)
        out = open('images/%s' % dd.split('/')[-1], 'wb')
        out.write(t.content)
        out.close()

когда выходит следующая ссылка у меня выходит ошибка:

Traceback (most recent call last):
  File "/opt/project/final-score.py", line 126, in <module>
    main()
  File "/opt/project/final-score.py", line 122, in main
    final_score()
  File "/opt/project/final-score.py", line 112, in final_score
    dd = '{url}{ext}{end}'.format(url=imgurg, ext=k['i']['n'], end='?id=Y6YPH3?&wid=1280&hei=877&fmt.png')
TypeError: string indices must be integers

Если я ссылку не генерирую то все нормально, может у меня json не может понять что это json?
Из — за чего ошибка?

решил проблему

try:
   for k in d['set']['item']:
      dd = '{url}{ext}{end}'.format(url=imgurg, ext=k['i']['n'], end='?id=Y6YPH3?&wid=1280&hei=877&fmt.png')
      t = requests.get(dd)
      out = open('images/%s' % dd.split('/')[-1], 'wb')
      out.write(t.content)
      out.close()
 except Exception as e:
 contine


Добавить комментарий