import requests
from bs4 import BeautifulSoup
URL = 'https://www.example'
HEADERS = {'user-agent': 'example'}
def get_html(url, params=None):
r = requests.get(url, headers=HEADERS, params=params)
return r
def get_content(html):
soup = BeautifulSoup(html, 'html.parser')
items = soup.find_all('span', class_='chart-element__information', limit=5)
names = []
for item in items:
names.append({
'name1': item.find('span', class_='chart-element__information__song text--truncate color--primary').get_text(),
'name2': item.find('span', class_='chart-element__information__artist text--truncate color--secondary').get_text()
})
result1 = ' - '.join([f'{value}' for key, value in names[0].items()])
result2 = ' '.join([f'{value}' for key, value in names[1].items()])
result3 = ' '.join([f'{value}' for key, value in names[2].items()])
result4 = ' '.join([f'{value}' for key, value in names[3].items()])
result5 = ' '.join([f'{value}' for key, value in names[4].items()])
def parse():
html = get_html(URL)
get_content(html.text)
parse()
def get_content(html):
soup = BeautifulSoup(html, 'html.parser')
items = soup.find_all('span', class_='chart-element__information', limit=5)
names = []
for item in items:
names.append({
'name1': item.find('span', class_='chart-element__information__song text--truncate color--primary').get_text(),
'name2': item.find('span', class_='chart-element__information__artist text--truncate color--secondary').get_text()
})
result1 = ' - '.join([f'{value}' for key, value in names[0].items()])
result2 = ' '.join([f'{value}' for key, value in names[1].items()])
result3 = ' '.join([f'{value}' for key, value in names[2].items()])
result4 = ' '.join([f'{value}' for key, value in names[3].items()])
result5 = ' '.join([f'{value}' for key, value in names[4].items()])
return result1, result2
html = get_html(URL)
result1, result2 = get_content(html.text)
from app import result1, result2
print(result1, result2)