From 75350fc36e8781cf190755043fc69a78bd32e934 Mon Sep 17 00:00:00 2001 From: Simon Egersand Date: Mon, 14 Aug 2017 11:05:14 +0200 Subject: [PATCH 1/3] Remove unused import --- pyhoroscope.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pyhoroscope.py b/pyhoroscope.py index fb9d269..794108e 100644 --- a/pyhoroscope.py +++ b/pyhoroscope.py @@ -1,6 +1,5 @@ import urllib.request from lxml import etree -import re #################################################################### # API From d3235a3fee2d298abd27e66d5d1e8511d833850e Mon Sep 17 00:00:00 2001 From: Simon Egersand Date: Mon, 14 Aug 2017 11:05:51 +0200 Subject: [PATCH 2/3] Trim horoscope output --- pyhoroscope.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pyhoroscope.py b/pyhoroscope.py index 794108e..5ea1f36 100644 --- a/pyhoroscope.py +++ b/pyhoroscope.py @@ -19,6 +19,7 @@ def get_todays_horoscope(sunsign): horoscope = str(tree.xpath( "//*[@id=\"daily\"]/div/div[1]/div[2]/p[1]/text()")) horoscope = horoscope.replace("[\"\\r\\n ", "").replace(" \\r\\n \\r\\n \"]", "") + horoscope = horoscope.strip() dict = { 'date': date, 'horoscope': horoscope, @@ -39,6 +40,7 @@ def get_weekly_horoscope(sunsign): horoscope = str(tree.xpath( "//*[@id=\"daily\"]/div/div[1]/div[2]/p[1]/text()")) horoscope = horoscope.replace("\\r\\n ", "").replace(" \\r\\n ", "").replace(" \\r\\n \\r\\n ", "").replace("['", "").replace("']", "") + horoscope = horoscope.strip() dict = { 'week': week, 'horoscope': horoscope, @@ -59,6 +61,7 @@ def get_monthly_horoscope(sunsign): horoscope = str(tree.xpath( "//*[@id=\"daily\"]/div/div[1]/div[2]/p[1]/text()[1]")) horoscope = horoscope.replace("\\r\\n ", "").replace("['", "").replace("']", "") + horoscope = horoscope.strip() dict = { 'month': month, 'horoscope': horoscope, @@ -79,6 +82,7 @@ def get_yearly_horoscope(sunsign): horoscope = str(tree.xpath( "//*[@id=\"daily\"]/div/div[1]/div[2]/p[1]/text()")) horoscope = horoscope.replace("['\\r\\n ", "").replace(" \\r\\n \\r\\n ", "").replace("[u'", "").replace("']", "").replace("\\xe2\\x80\\x99s", "") + horoscope = horoscope.strip() dict = { 'year': year, 'horoscope': horoscope, From 976258c0f4b9cb953dc53104596522f0750693b2 Mon Sep 17 00:00:00 2001 From: Simon Egersand Date: Mon, 14 Aug 2017 11:08:35 +0200 Subject: [PATCH 3/3] Make code conform to PEP-8 style guide --- pyhoroscope.py | 50 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/pyhoroscope.py b/pyhoroscope.py index 5ea1f36..4ab402f 100644 --- a/pyhoroscope.py +++ b/pyhoroscope.py @@ -1,23 +1,27 @@ import urllib.request from lxml import etree + #################################################################### # API #################################################################### class Horoscope: + def __init__(self): + pass @staticmethod def get_todays_horoscope(sunsign): url = "http://www.ganeshaspeaks.com/horoscopes/daily-horoscope/" + sunsign response = urllib.request.urlopen(url) - htmlparser = etree.HTMLParser() - tree = etree.parse(response, htmlparser) + html_parser = etree.HTMLParser() + tree = etree.parse(response, html_parser) date = str(tree.xpath( - "//*[@id=\"daily\"]/div/div[1]/div[1]/div[2]/div/p/text()")) + "//*[@id=\"daily\"]/div/div[1]/div[1]/div[2]/div/p/text()")) date = date.replace("['", "").replace("']", "").replace("['(", "").replace(")']", "") + horoscope = str(tree.xpath( - "//*[@id=\"daily\"]/div/div[1]/div[2]/p[1]/text()")) + "//*[@id=\"daily\"]/div/div[1]/div[2]/p[1]/text()")) horoscope = horoscope.replace("[\"\\r\\n ", "").replace(" \\r\\n \\r\\n \"]", "") horoscope = horoscope.strip() dict = { @@ -32,14 +36,16 @@ def get_todays_horoscope(sunsign): def get_weekly_horoscope(sunsign): url = "http://www.ganeshaspeaks.com/horoscopes/weekly-horoscope/" + sunsign response = urllib.request.urlopen(url) - htmlparser = etree.HTMLParser() - tree = etree.parse(response, htmlparser) + html_parser = etree.HTMLParser() + tree = etree.parse(response, html_parser) week = str(tree.xpath( - "//*[@id=\"daily\"]/div/div[1]/div[1]/div[2]/div/p/text()")) - week = week.replace("['", "").replace("[u'\\n", "").replace("']", "").replace("\\u2013", "-") + "//*[@id=\"daily\"]/div/div[1]/div[1]/div[2]/div/p/text()")) + week = week.replace("['", "").replace("[u'\\n", "").replace("']", "").replace("\\u2013", + "-") horoscope = str(tree.xpath( - "//*[@id=\"daily\"]/div/div[1]/div[2]/p[1]/text()")) - horoscope = horoscope.replace("\\r\\n ", "").replace(" \\r\\n ", "").replace(" \\r\\n \\r\\n ", "").replace("['", "").replace("']", "") + "//*[@id=\"daily\"]/div/div[1]/div[2]/p[1]/text()")) + horoscope = horoscope.replace("\\r\\n ", "").replace(" \\r\\n ", "").replace( + " \\r\\n \\r\\n ", "").replace("['", "").replace("']", "") horoscope = horoscope.strip() dict = { 'week': week, @@ -53,13 +59,14 @@ def get_weekly_horoscope(sunsign): def get_monthly_horoscope(sunsign): url = "http://www.ganeshaspeaks.com/horoscopes/monthly-horoscope/" + sunsign response = urllib.request.urlopen(url) - htmlparser = etree.HTMLParser() - tree = etree.parse(response, htmlparser) + html_parser = etree.HTMLParser() + tree = etree.parse(response, html_parser) month = str(tree.xpath( - "//*[@id=\"daily\"]/div/div[1]/div[1]/div[2]/div/p/text()")) - month = month.replace("['", "").replace("\\r\\n ", "").replace("['\\n", "").replace("']", "") + "//*[@id=\"daily\"]/div/div[1]/div[1]/div[2]/div/p/text()")) + month = month.replace("['", "").replace("\\r\\n ", "").replace("['\\n", "").replace("']", + "") horoscope = str(tree.xpath( - "//*[@id=\"daily\"]/div/div[1]/div[2]/p[1]/text()[1]")) + "//*[@id=\"daily\"]/div/div[1]/div[2]/p[1]/text()[1]")) horoscope = horoscope.replace("\\r\\n ", "").replace("['", "").replace("']", "") horoscope = horoscope.strip() dict = { @@ -74,14 +81,17 @@ def get_monthly_horoscope(sunsign): def get_yearly_horoscope(sunsign): url = "http://www.ganeshaspeaks.com/horoscopes/yearly-horoscope/" + sunsign response = urllib.request.urlopen(url) - htmlparser = etree.HTMLParser() - tree = etree.parse(response, htmlparser) + html_parser = etree.HTMLParser() + tree = etree.parse(response, html_parser) year = str(tree.xpath( - "//*[@id=\"daily\"]/div/div[1]/div[1]/div[2]/div/p/text()")) + "//*[@id=\"daily\"]/div/div[1]/div[1]/div[2]/div/p/text()")) year = year.replace("['", "").replace("['\\n", "").replace("']", "") + horoscope = str(tree.xpath( - "//*[@id=\"daily\"]/div/div[1]/div[2]/p[1]/text()")) - horoscope = horoscope.replace("['\\r\\n ", "").replace(" \\r\\n \\r\\n ", "").replace("[u'", "").replace("']", "").replace("\\xe2\\x80\\x99s", "") + "//*[@id=\"daily\"]/div/div[1]/div[2]/p[1]/text()")) + horoscope = horoscope.replace("['\\r\\n ", "").replace(" \\r\\n \\r\\n ", + "").replace("[u'", "").replace( + "']", "").replace("\\xe2\\x80\\x99s", "") horoscope = horoscope.strip() dict = { 'year': year,