forked from tapaswenipathak/Horoscope-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyhoroscope.py
102 lines (89 loc) · 3.98 KB
/
pyhoroscope.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import urllib.request
from lxml import etree
####################################################################
# 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)
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()"))
date = date.replace("['", "").replace("']", "").replace("['(", "").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 \"]", "")
horoscope = horoscope.strip()
dict = {
'date': date,
'horoscope': horoscope,
'sunsign': sunsign
}
return dict
@staticmethod
def get_weekly_horoscope(sunsign):
url = "https://www.ganeshaspeaks.com/horoscopes/weekly-horoscope/" + sunsign
response = urllib.request.urlopen(url)
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",
"-")
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,
'sunsign': sunsign
}
return dict
@staticmethod
def get_monthly_horoscope(sunsign):
url = "https://www.ganeshaspeaks.com/horoscopes/monthly-horoscope/" + sunsign
response = urllib.request.urlopen(url)
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("']",
"")
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,
'sunsign': sunsign
}
return dict
@staticmethod
def get_yearly_horoscope(sunsign):
url = "https://www.ganeshaspeaks.com/horoscopes/yearly-horoscope/" + sunsign
response = urllib.request.urlopen(url)
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()"))
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", "")
horoscope = horoscope.strip()
dict = {
'year': year,
'horoscope': horoscope,
'sunsign': sunsign
}
return dict