Created
August 16, 2012 23:13
-
-
Save melpomene/3374398 to your computer and use it in GitHub Desktop.
Vad får man kommentera på DN.se
This file contains 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
""" To run first | |
touch old_url.txt | |
touch data.txt | |
requires requests and feedparser (available through pip) | |
""" | |
import requests | |
import feedparser | |
rss = 'http://www.dn.se/nyheter/m/rss/senaste-nytt' | |
old_urls_file = 'old_url.txt' | |
data_file = 'data.txt' | |
def main(): | |
feed = feedparser.parse(rss) | |
old_str = open(old_urls_file, 'r').read() | |
old = open(old_urls_file, "a") | |
new = open(data_file, 'a') | |
for item in feed['items']: | |
if item['link'] not in old_str: | |
comment = check_url_for_comment(item['link']) | |
old.write(item['link']+u"\n") | |
new.write((comment + u"\t " + item['title'] + u"\t " + item['date'] + u"\t " + item['link'] + "\n").encode("utf-8")) | |
def check_url_for_comment(url): | |
r = requests.get(url) | |
if r.status_code == 200: | |
body = r.content | |
if 'class="comments"' in body: | |
return u"True" | |
else: | |
return u"False" | |
else: | |
raise Exception("Unable to access DN.se") | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment