-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBiography.py
More file actions
160 lines (139 loc) · 5.89 KB
/
Biography.py
File metadata and controls
160 lines (139 loc) · 5.89 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#imports
from bs4 import BeautifulSoup
from urllib.request import urlopen
import urllib
import re
import numpy as np
import pandas as pd
from PIL import Image
import requests
from io import BytesIO
header = {'User-Agent': 'Mozilla/5.0'}
events_Pic_Dic = {} # add pictures for each index
Actors_event = [] # {event year, event name , (optional) Description}
###this class gets biography for singers using the url of them###
class BiograpyGetter():
def __init__(self,url,discography_url):
# Query the website and return the html to the variable 'page'
page = urlopen(url)
discograph_page=urlopen(discography_url)
# Parse the html in the 'page' variable, and store it in Beautiful Soup format
self.singerSoup = BeautifulSoup(page, 'lxml')
self.discograpySoup=BeautifulSoup(discograph_page, 'lxml')
#add dictionary of events and their index
self.events_Dic = {"Born": 1, "Death": 2, "Marrige": 3, "Divorce": 4, "Single": 5,
"Album": 6} # event-index
#get picture
def getSingerPicture(self):
images = self.singerSoup.select('table.infobox a.image img[src]')
return images[0]['src']
#get birthdate
def getSingerBirthDate(self):
try:
right_table = self.singerSoup.find('table', class_='infobox biography vcard')
return [right_table.find('span', class_='bday').string[0:4],self.events_Dic['Born'],"Born"]
except:
return []
#get spouces of actor- marrige date and divorce date
def getSingerSpouses(self):
try:
listSpouses = []
right_table = self.singerSoup.find('table', class_='infobox biography vcard')
listCells = (right_table.find_all('td'))
for i in range(0, len(listCells)):
if listCells[i].find_all("abbr"):
lst = listCells[i].find_all("div")
for marrige in lst:
str = marrige.text
name = str[0:str.index('(')]
stop_index = 0 # for get married year
flag = False
if 'div' in str:
flag = True
stop_index = str.index(';')
else:
stop_index = str.index(')')
start_index=str.index('m.')
marrige_year = str[start_index+3: stop_index]
listSpouses.append([marrige_year, self.events_Dic["Marrige"], name])
if flag: # divorced
div_year = str[str.index('div.')+5:str.index(')')]
listSpouses.append([div_year, self.events_Dic["Divorce"], name])
break
return listSpouses
except:
return []
#get main singles og actor
def getSingerSingles(self):
for caption in self.discograpySoup.find_all('caption'):
if 'singles as lead artist' in caption.get_text() or 'List of singles' in caption.get_text():
table = caption.find_parent('table')
break
cells=[]
for tr in table.find_all('tr'):
cells.append(tr)
singles_dic=[] #year-5(singles)-name
#remove the headlines
cells.pop(0)
cells.pop(0)
i=0
while i< len(cells):
year=(cells[i].find('td').getText())
# remove /n
year=year[0:len(year)-1]
#get number of singles in the same year
if "span" in str(cells[i].find('td')):
index=int(str(cells[i].find('td'))[13:14])
else:
index=1
for j in range(0,index):
if cells[i].find('th')!=None:
name=cells[i].find('th').getText()
#remove /n
name = name[0:len(name) - 1]
if "(" in name:
name=name[1:name.index("(")-1]+name[name.index("("):len((name))]
if 'featuring' in name[name.index("("):len(name)] or 'with' in name[name.index("("):len(name)]:#need to remove all ()
name=name[0:name.index("(")]
elif "[" in name:
name = name[1:name.index("[") - 1] + name[name.index("["):len((name))]
if 'Z' in name[name.index("["):len(name)] or 'Q' in name[name.index("["):len(name)] or 'T' in name[name.index("["):len(name)] or 'R' in name[name.index("["):len(name)]:
name=name[0:name.index("[")]
else:
name=name[1:len(name)-1]
singles_dic.append([year,self.events_Dic["Single"],name])
i+=1
else:
i+=1
return singles_dic
#get albums of singer
def getSingerStudioAlbums(self):
for caption in self.discograpySoup.find_all('caption'):
if 'List of studio albums' in caption.get_text():
table = caption.find_parent('table')
break
cells=[]
for tr in table.find_all('tr'):
cells.append(tr)
albums_dic=[]
cells.pop(0)
cells.pop(0)
i=0
while i< len(cells):
year=(cells[i].find('td').getText())
year=year.split('\n')[1].split(' ')
if 'Released' in year[0]:
year=year[3]
else:
break
#make sure there is no other chars
if (len(year) > 4):
year = year[0:4]
if cells[i].find('th')!=None:
name=cells[i].find('th').getText()
name = name[0:len(name) - 1]
albums_dic.append([year,self.events_Dic["Album"],name])
i+=1
else:
i+=1
return albums_dic