View Single Post
Old 10-01-2010, 12:49 AM   #8
jefferson_frantz
Member
jefferson_frantz began at the beginning.
 
jefferson_frantz's Avatar
 
Posts: 14
Karma: 12
Join Date: Jan 2009
Location: Lima, Perú
Device: Kindle 2 and Sony Reader PRS 505
Thanks Tony for your help!!!
I tried your first suggestion, but the text didn't move below the image
But, this gave me an idea jeje
So, after some trial and error i found the solution ... possibly not the finest, but it works for me

Here is my new recipe:

Spoiler:

Code:
from calibre.web.feeds.recipes import BasicNewsRecipe
from BeautifulSoup import BeautifulSoup, Tag

class RevistaMuyInteresante(BasicNewsRecipe):

    title       = 'Revista Muy Interesante'
    __author__  = 'Jefferson Frantz'
    description = 'Revista de divulgacion'
    timefmt = ' [%d %b, %Y]'
    language = 'es_ES'

    no_stylesheets = True


    #then we add our own style(s) like this:
    extra_css = '''
                       .contentheading{font-weight: bold}
                       p {font-size: 4px;font-family: Times New Roman;}
                    '''

    ###########################################################
    #this right here gets rid of all the inline styles that prevent extra_css from working a lot
    #of times....
    ###########################################################
    def preprocess_html(self, soup):
            for item in soup.findAll(style=True):
               del item['style']
            return soup

    def preprocess_html(self, soup):
            for img_tag in soup.findAll('img'):
                parent_tag = img_tag.parent
                if parent_tag.name == 'td':
                    if not parent_tag.get('class') == 'txt_articulo': break
                    imagen = img_tag
                    new_tag = Tag(soup,'p')
                    img_tag.replaceWith(new_tag)
                    div = soup.find(attrs={'class':'article_category'})
                    div.insert(0,imagen)
            return soup

    keep_only_tags = [dict(name='div', attrs={'class':['article']}),dict(name='td', attrs={'class':['txt_articulo']})]

    remove_tags        = [
                             dict(name=['object','link','script','ul'])
                            ,dict(name='div', attrs={'id':['comment']})
                            ,dict(name='td', attrs={'class':['buttonheading']})
                            ,dict(name='div', attrs={'class':['tags_articles']})
                         ]

    remove_tags_after = dict(name='div', attrs={'class':'tags_articles'})


    #TO GET ARTICLES IN SECTION
    def nz_parse_section(self, url):
            soup = self.index_to_soup(url)
            div = soup.find(attrs={'class':'contenido'})
            current_articles = []
            for x in div.findAllNext(attrs={'class':['headline']}):
                    a = x.find('a', href=True)
                    if a is None:
                        continue
                    title = self.tag_to_string(a)
                    url = a.get('href', False)
                    if not url or not title:
                        continue
                    if url.startswith('/'):
                         url = 'http://www.muyinteresante.es'+url
#                    self.log('\t\tFound article:', title)
#                    self.log('\t\t\t', url)
                    current_articles.append({'title': title, 'url':url,
                        'description':'', 'date':''})

            return current_articles


    # To GET SECTIONS
    def parse_index(self):
            feeds = []
            for title, url in [
                ('Historia',
                 'http://www.muyinteresante.es/historia-articulos'),
             ]:
               articles = self.nz_parse_section(url)
               if articles:
                   feeds.append((title, articles))
            return feeds


Thanks again!

PS: The solution for the title with the extra_css works like a charm

Quote:
Originally Posted by TonytheBookworm View Post
as for the image thing do something like this
Spoiler:

Code:
def preprocess_html(self, soup):
        for img_tag in soup.findAll('img'):
            parent_tag = img_tag.parent
            if parent_tag.name == 'a':
                new_tag = Tag(soup,'p')
                new_tag.insert(0,img_tag)
                parent_tag.replaceWith(new_tag)
            elif parent_tag.name == 'p':
                if not self.tag_to_string(parent_tag) == '':
                    new_div = Tag(soup,'div')
                    new_tag = Tag(soup,'p')
                    new_tag.insert(0,img_tag)
                    parent_tag.replaceWith(new_div)
                    new_div.insert(0,new_tag)
                    new_div.insert(1,parent_tag)
        return soup


and as for the bold title or whatever you add extra_css
so lets say your title was in a <div class='title'>..... </div> tag and you wanted it different
you would do this:
Spoiler:

Code:
#first we need to turn off style sheets so we do this:
no_stylesheets = True

#then we add our own style(s) like this:
extra_css = '''
                   
                   .Title{font-weight: bold; font-size: xx-large}
                   p {font-size: 4px;font-family: Times New Roman;}
                '''



###########################################################
#this right here gets rid of all the inline styles that prevent extra_css from working a lot 
#of times....
###########################################################
def preprocess_html(self, soup):
        for item in soup.findAll(style=True):
           del item['style']
        return soup

Last edited by jefferson_frantz; 10-01-2010 at 12:52 AM.
jefferson_frantz is offline   Reply With Quote