-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScrape2.py
171 lines (138 loc) · 6.11 KB
/
Scrape2.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
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
159
160
161
162
163
164
165
166
167
import string
import urllib2
import re
nameOf1 = 'Sridevi'
nameOf2 = 'Anil_Kapoor'
str1 = "http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=jsonfm&titles="+ nameOf1 +"&rvsection=0"
str2 = "http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=jsonfm&titles="+ nameOf2 +"&rvsection=0"
url1 = urllib2.urlopen(str1);
url2 = urllib2.urlopen(str2);
orig = url1.read();
#print orig;
#print type(k);
rela = ["SPOUSE", "CHILD", "RELATIVE", "NAME"]
help1_relation = []
help2_relation = []
size = 4
list_1 = [[] for x in range(size)]
list_2 = [[] for x in range(size)]
for main_number in range(1,3):
if main_number==1:
url1 = urllib2.urlopen(str1);
orig = url1.read();
else:
url2 = urllib2.urlopen(str2);
orig = url2.read();
pos1 = orig.find('Infobox')
#print pos1;
temp = orig[pos1:];
pos2 = temp.find("'''");
#print pos2;
info_text = orig[pos1:pos2+pos1]
#print info_text;
pos_name = info_text.find('name');
if(pos_name != -1):
#print("NAME :"),
name = info_text[pos_name + len('name ='):]
for i in range(len(name)):
if ord(name[i])>=65 and ord(name[i])<=90:
break;
name = name[i: (name.find('n|')) -1 ]
#name = format_name(info_text[pos_name+7:], len(info_text[pos_name+7:]))
#print name
if main_number==1:
list_1[3].append(name)
else:
list_2[3].append(name)
#name = info_text[: (info_text[pos_name:].find('n|'))]
#print name[0:len(name)-1]
#print name[0:len(name)-1]
pos_relative = info_text.find('relatives');
if(pos_relative != -1):
#print("RELATIVE :"),
#print pos_relative + len('relatives =')
#print (info_text[pos_relative:].find('n|')) + pos_relative + 100
relative = info_text[pos_relative + len('relatives ='): (info_text[pos_relative:].find('n|')) + pos_relative+ 100]
#assuming maximum 4 relatives
#print relative
n1 = relative.find('[[');
n2 = relative.find(']]');
#print relative[n1+2:n2]
if main_number==1:
list_1[2].append(relative[n1+2:n2])
sk1 = relative.find("(");
sk2 = relative.find(")");
help1_relation.append(relative[sk1+1:sk2])
else:
list_2[2].append(relative[n1+2:n2])
sk1 = relative.find("(");
sk2 = relative.find(")");
help2_relation.append(relative[sk1+1:sk2])
if n2!=-1:
relative = relative[n2+2:]
n1 = relative.find('[[');
n2 = relative.find(']]');
#print relative[n1+2:n2]
if main_number==1:
list_1[2].append(relative[n1+2:n2])
sk1 = relative.find("(");
sk2 = relative.find(")");
help1_relation.append(relative[sk1+1:sk2])
else:
list_2[2].append(relative[n1+2:n2])
sk1 = relative.find("(");
sk2 = relative.find(")");
help2_relation.append(relative[sk1+1:sk2])
#print relative[0:len(relative)-1]
pos_spouse = info_text.find('spouse');
if(pos_spouse != -1):
#print("SPOUSE :"),
spouse = info_text[pos_spouse + len('spouse ='): (info_text[pos_spouse:].find('n|')) + len('spouse =')+pos_spouse]
n1 = spouse.find('[[');
n2 = spouse.find(']]');
#print spouse[n1+2:n2]
if main_number==1:
list_1[0].append(spouse[n1+2:n2])
else:
list_2[0].append(spouse[n1+2:n2])
regex = r'[' + "*" + r']';
pattern = re.compile(regex)
pos_parents = info_text.find('parents');
if(pos_parents != -1):
#print("PARENTS :"),
parents = info_text[pos_parents + len('parents ='): (info_text[pos_parents:].find('n|')) + pos_parents]
n1 = parents.find('[[');
n2 = parents.find(']]');
#print parents[n1+2:n2]
tit = re.findall(pattern, parents)
#print tit
#print parents[0:len(parents)-1]
pos_children = info_text.find('children');
if(pos_children != -1):
#print("CHILDREN :"),
children = info_text[pos_children + len('children ='): (info_text[pos_children:].find('n|')) + pos_children]
n1 = children.find('[[');
n2 = children.find(']]');
#print children[n1+2:n2]
if main_number==1:
list_1[1].append(children[n1+2:n2])
else:
list_2[1].append(children[n1+2:n2])
#print children[0:len(children)-1]
#print "LIST-1",
#print list_1
#print help1_relation
#print "LIST-2",
#print list_2
#print help2_relation
for i in range(size):
for j in range(size):
for k in range(len(list_1[i])):
for l in range(len(list_2[j])):
if list_1[i][k] == list_2[j][l]:
if i==3:
print nameOf1 + " is " + rela[j] + " of " + nameOf2
elif j==3:
print rela[i] + " of " + nameOf1 + " is " + nameOf2
else:
print rela[i] + " of " + nameOf1 + " is " + rela[j] + " of " + nameOf2