![]() |
#1 |
Zealot
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 126
Karma: 50000
Join Date: Mar 2015
Device: none
|
MIT Technology Review print/bimonthly
Could we make an alternate version of the MIT Technology Review that only grabs the print edition articles? http://www.technologyreview.com/magazine/2015/03/
Last edited by truth1ness; 04-03-2015 at 12:40 AM. |
![]() |
![]() |
![]() |
#2 |
Zealot
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 126
Karma: 50000
Join Date: Mar 2015
Device: none
|
Stuck
I'm attempting to make my first recipe for this but I'm stuck.
I've used The Atlantic recipe as a template and below is my code so far. The "for tag in col.findAll" line is where I think I'm stuck, I can't get it to run the For loop even once no matter what I put in. I printed out "col" and tried all different combinations of "class" names I saw in the output (throwing everything in by the end) but the "Print "START OF TAG"" never gets triggered and I get the error "ValueError: No articles found, aborting". Any help to proceed would be appreciated. Code:
#!/usr/bin/env python2 from __future__ import unicode_literals __license__ = 'GPL v3' __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>' ''' technologyreview.com ''' import re from calibre.web.feeds.news import BasicNewsRecipe class TheAtlantic(BasicNewsRecipe): title = 'MIT Technology Review' __author__ = '' description = '' INDEX = 'http://www.technologyreview.com/magazine/' language = 'en' encoding = 'utf-8' """keep_only_tags = [ {'attrs':{'class':['article-header', 'article-body', 'article-magazine']}}, ] remove_tags = [ {'name': ['meta', 'link', 'noscript']}, {'attrs':{'class':['offset-wrapper', 'ad-boxfeatures-wrapper']}}, {'attrs':{'class':lambda x: x and 'article-tools' in x}}, {'src':lambda x:x and 'spotxchange.com' in x}, ] no_stylesheets = True preprocess_regexps = [ (re.compile(r'<script\b.+?</script>', re.DOTALL), lambda m: ''), (re.compile(r'^.*<html', re.DOTALL|re.IGNORECASE), lambda m: '<html'), ] def print_version(self, url): return url + '?single_page=true'""" def parse_index(self): soup = self.index_to_soup(self.INDEX) col = soup.find(attrs={'class':'view-content'}) current_section, current_articles = None, [] feeds = [] print col print "END OF COL" for tag in col.findAll(name=['h2', 'li'], attrs={'class':['col', 'content-block', 'wrapper', ' ', 'image', 'content-block in-this-issue no-border']}): print "START OF TAG" print tag if tag.name == 'h2': if current_section and current_articles: feeds.append((current_section, current_articles)) current_section = self.tag_to_string(tag).capitalize() current_articles = [] self.log('Found section:', current_section) elif current_section: a = tag.find('a', href=True) if a is not None: title, url = self.tag_to_string(a), a['href'] if title and url: p = tag.find('p', attrs={'class':'river-dek'}) desc = self.tag_to_string(p) if p is not None else '' current_articles.append({'title':title, 'url':url, 'description':desc}) self.log('\tArticle:', title, '[%s]' % url) self.log('\t\t', desc) if current_section and current_articles: feeds.append((current_section, current_articles)) return feeds |
![]() |
![]() |
Advert | |
|
![]() |
#3 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,338
Karma: 27182818
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
Remove the class constraint in the findAll. If you want to use class then you have to remember that it must match the entire value of the class attribute, not a single class.
|
![]() |
![]() |
![]() |
#4 | |
Zealot
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 126
Karma: 50000
Join Date: Mar 2015
Device: none
|
Thanks. Removing the classes got the for loop printing. I'm not really clear what they were for.
Having trouble in another section. No matter what "a" keeps its value of "None". Here is the output of one of the tags: Quote:
Here's my latest attempt (not much progress): Code:
#!/usr/bin/env python2 from __future__ import unicode_literals __license__ = 'GPL v3' __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>' ''' technologyreview.com ''' import re from calibre.web.feeds.news import BasicNewsRecipe class TheAtlantic(BasicNewsRecipe): title = 'MIT Technology Review' __author__ = '' description = '' INDEX = 'http://www.technologyreview.com/magazine/' language = 'en' encoding = 'utf-8' """keep_only_tags = [ {'attrs':{'class':['article-header', 'article-body', 'article-magazine']}}, ] remove_tags = [ {'name': ['meta', 'link', 'noscript']}, {'attrs':{'class':['offset-wrapper', 'ad-boxfeatures-wrapper']}}, {'attrs':{'class':lambda x: x and 'article-tools' in x}}, {'src':lambda x:x and 'spotxchange.com' in x}, ] no_stylesheets = True preprocess_regexps = [ (re.compile(r'<script\b.+?</script>', re.DOTALL), lambda m: ''), (re.compile(r'^.*<html', re.DOTALL|re.IGNORECASE), lambda m: '<html'), ] def print_version(self, url): return url + '?single_page=true'""" def parse_index(self): soup = self.index_to_soup(self.INDEX) col = soup.find(attrs={'class':'view-content'}) current_section, current_articles = None, [] feeds = [] print col print "END OF COL" for tag in col.findAll(name=['a', 'h2'], attrs={}): print "PRINT TAG ", tag print "PRINT TAG.NAME", tag.name print "PRINT CURRENT_SECTION ", current_section if tag.name == 'h2': if current_section and current_articles: feeds.append((current_section, current_articles)) current_section = self.tag_to_string(tag).capitalize() current_articles = [] self.log('Found section:', current_section) elif current_section: a = tag.find('a', href=True) print "PRINT A ", a if a is not None: title, url = self.tag_to_string(a), a['href'] if title and url: p = tag.find('p', attrs={'class':'river-dek'}) desc = self.tag_to_string(p) if p is not None else '' current_articles.append({'title':title, 'url':url, 'description':desc}) self.log('\tArticle:', title, '[%s]' % url) self.log('\t\t', desc) if current_section and current_articles: feeds.append((current_section, current_articles)) return feeds Last edited by truth1ness; 04-04-2015 at 03:13 AM. |
|
![]() |
![]() |
![]() |
#5 | |
Zealot
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 126
Karma: 50000
Join Date: Mar 2015
Device: none
|
The other thing is they use H2 headers for both the magazine Section (ie IN THIS ISSUE: ANALYSIS) as well as name of regular columns (ie Letter from the editor). How would I differentiate these?
For example Quote:
Last edited by truth1ness; 04-04-2015 at 04:47 AM. |
|
![]() |
![]() |
Advert | |
|
![]() |
#6 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,338
Karma: 27182818
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
tag is already an <a> tag, so tag.find('a') will always return None since there is no nested <a> tag inside the <a> tag.
|
![]() |
![]() |
![]() |
#7 |
Zealot
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 126
Karma: 50000
Join Date: Mar 2015
Device: none
|
Made the recipe! This is my first one so if you can give it a look over and let me know if anything could be improved and see if it's good enough to add to the repository.
This doesn't replace the other "Technology Review" one which is the RSS/website version whereas this one actually takes in the bimonthly magazine. I tested it on the last three months and seemed to work well. |
![]() |
![]() |
![]() |
#8 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,338
Karma: 27182818
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
|
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
recipe for Technology Review - german | schuster | Recipes | 1 | 06-05-2016 07:17 AM |
Technology Review (United States) Updated | bcollier | Recipes | 1 | 10-25-2013 10:44 AM |
Newbie on technology want to print an e-book | bassmanwa | Introduce Yourself | 4 | 07-02-2011 04:23 AM |
txtr reader vorgestellt in Technology Review 03/09 | Alexander Turcic | Andere Lesegeräte | 9 | 03-19-2009 10:16 AM |
Sony Reader reviewed by MIT Technology Review | Bob Russell | Sony Reader | 38 | 11-09-2006 05:04 PM |