我尝试改进calibre的配方,并用当前报纸的封面图片替换默认的封面图片。
要走的路与get_cover_url
(链接)有关。
有两个问题:
- 封面图片的 URL 每天都在变化。
- 我对python几乎一无所知。
我希望有这样的解决方案(在伪代码中):
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;
有没有办法在 python*) 中实现这一点(仅使用 python 标准库)?
*) Calibre-Recipes 似乎是 python 代码
[编辑] 这是我的一个朋友提供的解决方案:
#!/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)