Last active
August 29, 2015 14:11
-
-
Save lozhn/bdaa66506bd902fd2778 to your computer and use it in GitHub Desktop.
Грабим engshop.ru на предмет всех 154 сонетов Шекспира в ПДФничек и делаем 2 версии : оригинал / оригинал + перевод Маршака
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
# -*- coding: utf-8 -*- | |
import lxml.html | |
import pdfkit | |
import markdown | |
base_url = "http://engshop.ru/shekspir-sonet-%i-na_anglieskom/" | |
options = { | |
# 'page-size': 'Letter', | |
'encoding': "UTF-8", | |
'no-outline': None, | |
} | |
# только енг | |
l = u"<html><pre>" | |
for i in xrange(1,155): | |
page = lxml.html\ | |
.parse("http://engshop.ru/shekspir-sonet-%i-na_anglieskom/" % i)\ | |
.xpath('//pre/text()') | |
x = page[0].split('\n') | |
# говнокод говнокод говнокод, но как-то так быстро получилось и работает | |
# самые траблы были с тем как wkhtmltopdf переводит html в pdf, неправославно как-то | |
# в итоге вот такие костыли | |
a = "<p>Sonet no. %i </p>" % i | |
for i in x: | |
a+="<p>"+i+"</p>" | |
a+="<br>" | |
l+=a | |
l+="</pre></html>" | |
pdfkit.from_string(l, 'eng.pdf', options=options) | |
# тут мы делаем енг+рус | |
l = u"<html><pre>" | |
for i in xrange(1,155): | |
page = lxml.html\ | |
.parse("http://engshop.ru/shekspir-sonet-%i-na_anglieskom/" % i)\ | |
.xpath('//pre/text()') | |
x = page[0].split('\r') | |
y = page[1].split('\r') | |
a = "<table><tr><p>Sonet no. %i </p></tr>" % i | |
for i in xrange(len(x)): | |
# еще один костыль, в русском(а может в английском, не смотрел) переводе где-то строк меньше(почему-то) | |
aa = x.pop(0) if len(x) else "" | |
bb = y.pop(0) if len(y) else "" | |
a+="<tr><td><p>"+aa+"</p></td><td>"+"<p>"+bb+"</p></td></tr>" | |
a+="</table><br>" | |
l+=a | |
l+="</pre></html>" | |
pdfkit.from_string(l, 'eng-rus.pdf', options=options) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment