0
import urllib.request
from bs4 import BeautifulSoup

def get_html(url):
    response = urllib.request.urlopen(url)
    return response.read()


def parse(html):
    soup = BeautifulSoup(html)
    projects = []
    table = soup.find('div', class_= 'wrap')
    rows = table.find_all('div', class_='listing_wr')
    for i in rows:
        projects.append({
            'title' : i.a.text,
            'Лет': i.super
        })
    for i in projects:
        print(i)


def main():
    parse(get_html('http://kakoysegodnyaprazdnik.ru/'))

if __name__ == '__main__':
    main()

Вообщем мне нужно получить все названия и лет праздников с сайта, текст я получил. Но всего-лишь 3, а лет вообще не могу получить помогите пожалуйста(

1 ответ 1

1

Это немного грубо, но думаю делает, что вам надо:

import urllib.request
from bs4 import BeautifulSoup

def get_html(url):
    response = urllib.request.urlopen(url)
    return response.read()


def parse(html):
    soup = BeautifulSoup(html, "lxml")
    projects = []
    rows = soup.find_all('div', class_='main')
    for row in rows:
        spans = row.find_all('span')
        spans_year = row.find_all('span', class_='super')
        print(spans[0].text, spans_year[0].text if spans_year else "")


def main():
    parse(get_html('http://kakoysegodnyaprazdnik.ru/'))

if __name__ == '__main__':
    main()

Суть в том, что я ищу по классу все div c названием и потом выбираю span с названием праздника - он вроде всегда первый и потом ищу span с class="super", который соответствует годам. И если этот span есть, то вывоже его на экран.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.