View Single Post
Old 10-15-2020, 12:31 PM   #2
gourav
Member
gourav doesn't littergourav doesn't litter
 
Posts: 14
Karma: 132
Join Date: Aug 2014
Device: Kindle Paperwhite 7th Gen
I gave it a shot and this is what I could come up with:

Code:
#!/usr/bin/env python
# vim:fileencoding=utf-8
from calibre.web.feeds.news import BasicNewsRecipe

class NYTCooking(BasicNewsRecipe):
    title          = 'NYT Cooking'
    oldest_article = 2
    max_articles_per_feed = 30
    auto_cleanup   = True
    auto_cleanup_keep = '//div[@class="recipe-intro"]|'\
                        '//div[@class="recipe-instructions"]|'
    
    def parse_index(self):
        url = 'https://cooking.nytimes.com/topics/what-to-cook-this-week'
        br = self.get_browser()
        response = br.open(url)
        html = response.read()
        soup = BeautifulSoup(html)

        articles = soup.find_all('div', class_='card-info-wrapper')
        feed = []

        for i in articles:
            article = {}
            article['url'] = i.find('a')['href']
            if article['url'].startswith('/'):
                article['url'] = 'https://cooking.nytimes.com' + article['url']
            article['title'] = i.find('h3').text.strip()
            author = i.find('p', class_='card-byline')
            article['author'] = author.text if author is not None else 'Unknown'
            feed.append(article)
            
        return [('Recipes', feed)]
I'm fairly new to this myself. So I was not able to get a clean output, but this works. Give it a shot and let me know whether it works for you or not.
gourav is offline   Reply With Quote