V
V
vladimir3282020-06-30 22:14:23
Python
vladimir328, 2020-06-30 22:14:23

How to concatenate python dictionaries?

import requests
from bs4 import BeautifulSoup

url = 'https://ria.ru/location_Novosibirsk/'
page = requests.get(url)
print(page.status_code)

news = []
data = []
clear_news = []

soup = BeautifulSoup(page.text,'html.parser')

news = soup.find_all('a', class_='list-item__title color-font-hover-only')
data = soup.find_all('div', class_='list-item__date')

for key in news:
    clear_news.append(key.string)
for value in data:
    clear_news.append(value.string)

print(clear_news)


how to merge dictionaries so that clear_news dictionary shows date:news?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2020-06-30
@vladimir328

You don't have dictionaries, you have lists.

news = soup.find_all('a', class_='list-item__title color-font-hover-only')
data = soup.find_all('div', class_='list-item__date')

keys = [item.string for item in news]
values = [item.string for item in data]

clear_news = dict(zip(keys, values))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question