-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscrapeData.py
227 lines (200 loc) · 7.89 KB
/
scrapeData.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# required libraries
import requests
from bs4 import BeautifulSoup
from bs4 import NavigableString
import csv
import pandas as pd
## global variables
#hashmaps to prevent duplicates
global table_map
global list_map
#set to check if a url is already visited
global url_set
list_map={}
table_map={}
url_set= set()
flag=True
# helper function to split a word
def split(word):
return [char for char in word]
# helper function to check if a tag is preceded by string
def surrounded_by_strings(tag):
if(tag != None):
return (isinstance(tag.previous_element, NavigableString))
# helper function to get the index of the required header name from a table
def getHeaderIndex(tables):
name_idx = None
type_idx = None
desc_idx = None
for table in tables:
head = table.find_all('th')
i=0;
for h in head:
if(any (item in h.text.split() for item in ['name', 'Name', 'NAME', 'title', 'Title', 'TITLE'])):
if(name_idx == None):
name_idx = i
else:
pass
if(any (item in h.text.split() for item in ['type', 'Type', 'TYPE'])):
if(type_idx == None):
type_idx = i
else:
pass
if(any (item in h.text.split() for item in ['description', 'Description', 'DESCRIPTION'])):
if(desc_idx == None):
desc_idx = i
else:
pass
i+=1;
return name_idx, desc_idx, type_idx
# function to extract data from a page that is arranged in table format
def getTableData(main, category):
tables = []
h3=None
h2=None
temph3=None
temph2=None
update = None
for i in main.find_all(True):
if(i.has_attr('class') and i['class'][0] == 'mw-headline' and i.text == 'See also'):
break
if(i.name == 'table'):
tables.append(i)
name_idx, desc_idx, type_idx = getHeaderIndex(tables)
for i in main.find_all(True):
if(i.has_attr('class') and i['class'][0] == 'mw-headline' and i.text == 'See also'):
break
if(i.name == 'h3' and len(split(i.text.split()))>1):
h3 = i.text.split('[')[0]
if(i.name == 'h2' and len(split(i.text.split()))>1):
h2 = i.text.split('[')[0]
if(temph3 != h3):
temph3 = h3
update = h3
if(temph2 != h2):
temph2 = h2
update = h2
for tr in i.find_all('tr'):
td = tr.find_all('td')
row = [i.text for i in td]
try:
name = row[name_idx]
name = name.replace('\n','')
description = row[desc_idx]
description = description.replace('\n','')
if(type_idx != None):
dish_type = row[type_idx]
dish_type = dish_type.replace('\n','')
else:
dish_type=update
table_map[name] = [description, category, dish_type]
except Exception as e:
name = None
description = None
return
# function to extract data from a page that is arranged in list format
def getListData(main, category):
h3=None
h2=None
for i in main.find_all(True):
if(i.has_attr('class') and i['class'][0] == 'mw-headline' and i.text == 'See also'):
break
if(i.name == 'h3'and len(split(i.text.split()))>1):
h3 = i.text.split('[')[0]
if(i.name == 'h2'and len(split(i.text.split()))>1):
h2 = i.text.split('[')[0]
if(h3 != None):
dish_type = h3
else:
dish_type = h2
for l in i.find_all('li'):
try:
ref = l.find('a')
except Exception as e:
ref = None
if(surrounded_by_strings(ref)):
pass
else:
try:
val = l.find('a')['href']
spl = val.split('/')
if(any (item in spl for item in ['wiki'])):
name = l.find('a')['title']
name = name.replace('\n','')
u = f'https://en.wikipedia.org/{val}'
if('File' not in u):
check=True
x=0
detail = requests.get(u)
data = BeautifulSoup(detail.content, 'html.parser')
cont = data.find('div', class_='mw-parser-output')
while(check):
description = cont.find_all('p')[x].get_text()
description = description.replace('\n','')
if(description):
check=False
else:
x+=1
if len(description) > 100:
description = description.partition('.')[0] + '.'
list_map[name] = [description, category, dish_type]
except Exception as e:
u = None
return
# open a csv file and add headers
output_file = open('all_dishes.csv', 'w')
csv_writer = csv.writer(output_file)
csv_writer.writerow(['Category', 'Name', 'Type', 'Description'])
## Web Scrape
#starting url
url = 'https://en.wikipedia.org/w/index.php?title=Special:Search&limit=500&offset=0&ns0=1&search=List+of+dishes+AND+dishes&advancedSearch-current={}'
#loop through all the pages of links
while(flag):
start = requests.get(url);
itrList = BeautifulSoup(start.content, 'html.parser');
for item in itrList.find_all('div', class_='mw-search-result-heading'):
if('List' in item.a.text and any(i.lower() in item.a.text.split() for i in ['dishes', 'foods'])):
cat = item.a.text.split('of')[1].strip()
if(cat != 'prepared foods'):
category = cat
try:
val = item.find('a')['href']
url = f'https://en.wikipedia.org{val}'
if url not in url_set:
url_set.add(url)
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
main = soup.find('div', class_='mw-parser-output')
res='List'
for i in main.find_all(True):
if(i.has_attr('class') and i['class'][0] == 'mw-headline' and i.text == 'See also'):
break
if(i.name == 'table'):
res='Table'
break
if(res == 'Table'):
getTableData(main, category)
else:
getListData(main, category)
except Exception as e:
pass
#page iterator and break condition
bottom_nav = itrList.find('p', class_='mw-search-pager-bottom')
itr = [i.text for i in bottom_nav.find_all('a')]
if 'next 500'in itr:
flag = True
link = bottom_nav.find('a', class_='mw-nextlink')['href']
url = f'https://en.wikipedia.org/{link}'
else:
flag = False
#write the data to csv file
for key in table_map:
value = table_map[key]
if(key != None and value[0] != None):
csv_writer.writerow([value[1], key, value[2], value[0]])
for key in list_map:
value = list_map[key]
if(key != None and value[0] != None):
csv_writer.writerow([value[1], key, value[2], value[0]])
# close the file
output_file.close()