Answer the question
In order to leave comments, you need to log in
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')
Answer the question
In order to leave comments, you need to log in
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)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question