B
B
badrbot2018-11-20 17:44:12
Python
badrbot, 2018-11-20 17:44:12

How to make sure there are no nested arrays?

There is a code:

import urllib.request
from bs4 import BeautifulSoup
import requests
import json

def get_link(url):#Получение ссылок на задания
    html = urllib.request.urlopen(url)
    soup = BeautifulSoup(html, 'html.parser').find('div', id='taskCont', class_='taskCont')
    links = []
    for i in soup.find_all('a', href=True):
        a = i['href']
        link = url + a
        s = link[:23]
        u = 'json/'
        k=link[23:]
        suk = s+u+k
        suk=suk.replace('#task?t=','/')
        links.append(suk)
    return links

def get_url_for_img(url):
    data = requests.get(url).json()
    kuim = []
    for i in data['editions']:
        im = [j['url'] for j in i['images']]
        print(im)
        for ji in im:
            kuim.append(ji)
    return kuim

url = ''
s = get_link(url)
a = []
for y in s:
    k = get_url_for_img(y)
    print(k)

and it returns this:
['/attachments/images/tasks/000/001/887/0002/5a6491b599d46.png']
['/attachments/images/tasks/000/001/887/0002/5a6491b5ad9f0.png', '/attachments/images/tasks/000/001/887/0002/5a6491b5adb74.png']
['/attachments/images/tasks/000/001/887/0002/5a6491b5add30.png', '/attachments/images/tasks/000/001/887/0002/5a6491b5aded4.png']
['/attachments/images/tasks/000/001/887/0002/5a6491b5ae09d.png', '/attachments/images/tasks/000/001/887/0002/5a6491b5ae2fc.png']
['/attachments/images/tasks/000/001/887/0002/5a6491b5ae4e1.png', '/attachments/images/tasks/000/001/887/0002/5a6491b5bacc9.png']
['/attachments/images/tasks/000/001/887/0002/5a6491b5baf0a.png', '/attachments/images/tasks/000/001/887/0002/5a6491b5bb09b.png', '/attachments/images/tasks/000/001/887/0002/5a6491b5bb278.png', '/attachments/images/tasks/000/001/887/0002/5a6491b5bb451.png']
['/attachments/images/tasks/000/001/887/0002/5a6491b5bb5f5.png', '/attachments/images/tasks/000/001/887/0002/5a6491b5bb785.png']
['/attachments/images/tasks/000/001/887/0002/5a6491b5bf857.png', '/attachments/images/tasks/000/001/887/0002/5a6491b5bfa5d.png']
['/attachments/images/tasks/000/001/887/0002/5a6491b5bfc1b.png', '/attachments/images/tasks/000/001/887/0002/5a6491b5bfdbb.png']
['/attachments/images/tasks/000/001/887/0002/5a6491b5a536e.png', '/attachments/images/tasks/000/001/887/0002/5a6491b5a54e1.png', '/attachments/images/tasks/000/001/887/0002/5a6491b5a5668.png']
['/attachments/images/tasks/000/001/887/0002/5a6491b5a57b0.png', '/attachments/images/tasks/000/001/887/0002/5a6491b5a5903.png', '/attachments/images/tasks/000/001/887/0002/5a6491b5a87ad.png']
['/attachments/images/tasks/000/001/887/0002/5a6491b5a898b.png', '/attachments/images/tasks/000/001/887/0002/5a6491b5a8b52.png']
['/attachments/images/tasks/000/001/887/0002/5a6491b5a8d1d.png', '/attachments/images/tasks/000/001/887/0002/5a6491b5a8eb4.png', '/attachments/images/tasks/000/001/887/0002/5a6491b5a9133.png']

and I need everything to be in order in one array:
['/attachments/images/tasks/000/001/887/0002/5a6491b599d46.png', '/attachments/images/tasks/000/001/887/0002/5a6491b5ad9f0.png', '/attachments/images/tasks/000/001/887/0002/5a6491b5adb74.png', '/attachments/images/tasks/000/001/887/0002/5a6491b5add30.png', '/attachments/images/tasks/000/001/887/0002/5a6491b5aded4.png']

The point is that each answer has a different number of links.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AWEme, 2018-11-20
@acrytzat

Why urllib and requests together?
If you do not change the logic of the program, then this part:

for y in s:
    k = get_url_for_img(y)
    print(k)

Can be replaced with:
Get a list with nested lists.
And with the help from itertools import chainyou can do this:
As a result, all links will be processed and will be in one list.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question