D
D
Dmitry Sakhnov2021-10-29 14:26:54
Python
Dmitry Sakhnov, 2021-10-29 14:26:54

How to insert an element at the beginning of each list value in Python?

Hello, I have been sitting on the problem for quite a long time, the tutorials do not help, but I did not find suitable answers on the Internet.

In general, I upload a list of links, but the href attribute contains only half of the link for each position in the list. At first, the https://www.bundestag.de

part is missing. I tried all the methods of working with lists that I found, but I can’t catch up with how to insert (blind) a necessary element in each value, I think the task is simple, but it puzzled me (

persons_url_list = []

for i in range(0, 740, 20):

    url =f"https://www.bundestag.de/ajax/filterlist/de/abgeordnete/862712-862712?limit=20&noFilterSet=true&offset={i}"

    q = requests.get(url)
    result = q.content

    soup = BeautifulSoup(result, 'lxml')
    persons = soup.find_all(class_="bt-open-in-overlay")
    for person in persons:
        person_page_url = person.get('href')

        persons_url_list.append(person_page_url)
 
with open('persons_url_list.txt', 'a') as file:
    for line in persons_url_list:
        file.write(f'{line}\n')

example of one of the links that should be listed https://www.bundestag.de/abgeordnete/biografien/A/...

617bd88bd634d888946686.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
datka, 2021-10-29
@SPECTRRODIUM

for i in range(0, 740, 20):
    main_url = "https://www.bundestag.de"
    url =f"https://www.bundestag.de/ajax/filterlist/de/abgeordnete/862712-862712?limit=20&noFilterSet=true&offset={i}"

    q = requests.get(url)
    result = q.content

    soup = BeautifulSoup(result, 'lxml')
    persons = soup.find_all(class_="bt-open-in-overlay")
    for person in persons:
        person_page_url = person.get('href')

        persons_url_list.append(main_url+person_page_url)

main_url = " https://www.bundestag.de "
persons_url_list.append(main_url+person_page_url)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question