Question

I try to improve a recipe for calibre and replace the default cover image with the cover image of the current newspaper issue.

The way to go has something to do with get_cover_url (link).

There are two problems:

  1. The URL of the cover image changes every day.
  2. I know virtually nothing about python.

I hope for a solution like this (in pseudo-code):

OPEN URL "http://epaper.derstandarddigital.at/";
coverElement = (SEARCH HTML-ELEMENT "<img>" WITH ID "imgPage2" AND CLASS "page");
coverUrl = (GET HTML-ATTRIBUTE "src" FROM coverElement);
RETURN coverUrl;

Would there be a way to achieve this in python*) (using only python standard libraries)?

*) Calibre-Recipes seem to be python code

[edit] here's the solution a friend of mine offered:

#!/usr/bin/env  python

import urllib
from time import strftime


def get_cover_url(self):
    highResolution = True

    date    = strftime("%Y/%Y%m%d")
    # it is also possible for the past
    #date    = '2012/20120503'

    urlP1   = 'http://epaper.derstandarddigital.at/'
    urlP2   = 'data_ep/STAN/' + date
    urlP3   = '/V.B1/'
    urlP4   = 'paper.htm'
    urlHTML = urlP1 + urlP2 + urlP3 + urlP4

    htmlF  = urllib.urlopen(urlHTML)
    htmlC  = htmlF.read()


    # URL EXAMPLE: data_ep/STAN/2012/20120504/V.B1/pages/A3B6798F-2751-4D8D-A103-C5EF22F7ACBE.htm
    # consists of part2 + part3 + 'pages/' + code
    # 'pages/' has length 6, code has lenght 36

    index   = htmlC.find(urlP2) + len(urlP2 + urlP3) + 6 
    code    = htmlC[index:index + 36]


    # URL EXAMPLE HIGH RESOLUTION: http://epaper.derstandarddigital.at/data_ep/STAN/2012/20120504/pagejpg/A3B6798F-2751-4D8D-A103-C5EF22F7ACBE_b.png
    # URL EXAMPLE LOW RESOLUTION: http://epaper.derstandarddigital.at/data_ep/STAN/2012/20120504/pagejpg/2AB52F71-11C1-4859-9114-CDCD79BEFDCB.png

    urlPic  = urlP1 + urlP2 + '/pagejpg/' + code

    if highResolution:
        urlPic  = urlPic + '_b'

    urlPic  = urlPic + '.png'

    return urlPic



if __name__ == '__main__':
    print get_cover_url(None)
Was it helpful?

Solution

You can use the lxml library to extract elements from an HTML document. The basic framework will look something like this:

import urllib
import lxml.html

fd = urllib.urlopen('http://epaper.derstandarddigital.at/')
doc = lxml.html.parse(fd)
matches = doc.xpath('//img[@id="imgPage2]')
if matches:
  print matches[0].get('src')

...however, the URL you've provided does not actually result in a document with any <img> tags. It is almost entirely a bunch of Javascript.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top