diff --git a/pyhoroscope.py b/pyhoroscope.py index d92dc89..4bac5dd 100644 --- a/pyhoroscope.py +++ b/pyhoroscope.py @@ -1,25 +1,29 @@ import urllib.request from lxml import etree -import re + #################################################################### # API #################################################################### class Horoscope: + def __init__(self): + pass @staticmethod def get_todays_horoscope(sunsign): url = "https://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 = { 'date': date, 'horoscope': horoscope, @@ -32,14 +36,17 @@ def get_todays_horoscope(sunsign): def get_weekly_horoscope(sunsign): url = "https://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, 'horoscope': horoscope, @@ -52,14 +59,16 @@ def get_weekly_horoscope(sunsign): def get_monthly_horoscope(sunsign): url = "https://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 = { 'month': month, 'horoscope': horoscope, @@ -72,14 +81,18 @@ def get_monthly_horoscope(sunsign): def get_yearly_horoscope(sunsign): url = "https://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, 'horoscope': horoscope,