-
Notifications
You must be signed in to change notification settings - Fork 2
/
scrap.py
executable file
·153 lines (115 loc) · 5.01 KB
/
scrap.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
#!/usr/local/bin/python
import os
import requests
import json
from wiktionaryparser import WiktionaryParser
NB = 'norwegian bokmål'
NN = 'norwegian nynorsk'
parser = WiktionaryParser()
#word = parser.fetch("klatrestativ", NB)
#file = open('./2_wiktionaryDump/NB/klatrestativ.json', 'w')
#file.write(json.dumps(word))
#file.close()
#exit(0)
def scrap(linesToScrap, languageToScrap):
for wordToScrap in linesToScrap:
# print(wordToScrap+"AWOO")
if wordToScrap == "": # Newline at the end of file
continue
if languageToScrap == NB:
try:
word = parser.fetch(wordToScrap, NB)
file = open('./2_wiktionaryDump/NB/'+wordToScrap+'.json', 'w')
file.write(json.dumps(word))
file.close()
print("Scraped word '"+wordToScrap+"' in language "+NB)
except:
print("Word '"+wordToScrap+"' does not appear to be defined in language "+NB)
if languageToScrap == NN:
try:
word = parser.fetch(wordToScrap, NN)
file = open('./2_wiktionaryDump/NN/'+wordToScrap+'.json', 'w')
file.write(json.dumps(word))
file.close()
print("Scraped word '"+wordToScrap+"' in language "+NN)
except:
print("Word '"+wordToScrap+"' does not appear to be defined in language "+NN)
return "yay"
scrap([line.rstrip('\n') for line in open('./1_wordlists/nb-NO_adjectives.txt')], NB)
scrap([line.rstrip('\n') for line in open('./1_wordlists/nb-NO_adverbs.txt')], NB)
scrap([line.rstrip('\n') for line in open('./1_wordlists/nb-NO_conjunctions.txt')], NB)
scrap([line.rstrip('\n') for line in open('./1_wordlists/nb-NO_determiners.txt')], NB)
scrap([line.rstrip('\n') for line in open('./1_wordlists/nb-NO_nouns.txt')], NB)
scrap([line.rstrip('\n') for line in open('./1_wordlists/nb-NO_numerals.txt')], NB)
scrap([line.rstrip('\n') for line in open('./1_wordlists/nb-NO_prepositions.txt')], NB)
scrap([line.rstrip('\n') for line in open('./1_wordlists/nb-NO_pronouns.txt')], NB)
scrap([line.rstrip('\n') for line in open('./1_wordlists/nb-NO_verbs.txt')], NB)
scrap([line.rstrip('\n') for line in open('./1_wordlists/nn-NO_adjectives.txt')], NN)
scrap([line.rstrip('\n') for line in open('./1_wordlists/nn-NO_adverbs.txt')], NN)
scrap([line.rstrip('\n') for line in open('./1_wordlists/nn-NO_conjunctions.txt')], NN)
scrap([line.rstrip('\n') for line in open('./1_wordlists/nn-NO_determiners.txt')], NN)
scrap([line.rstrip('\n') for line in open('./1_wordlists/nn-NO_nouns.txt')], NN)
scrap([line.rstrip('\n') for line in open('./1_wordlists/nn-NO_numerals.txt')], NN)
scrap([line.rstrip('\n') for line in open('./1_wordlists/nn-NO_prepositions.txt')], NN)
scrap([line.rstrip('\n') for line in open('./1_wordlists/nn-NO_pronouns.txt')], NN)
scrap([line.rstrip('\n') for line in open('./1_wordlists/nn-NO_verbs.txt')], NN)
print("Finished scrapping entries!")
#parser.set_default_language(NB) # No real use for it here
#print(json.dumps(parser.fetch("alvorligere",NB)))
#exit(0)
#wordToScrap = "behagelig"
exit(0)
from bs4 import BeautifulSoup
# <!--
# https://www.crummy.com/software/BeautifulSoup/bs4/doc/
##url = "https://en.wiktionary.org/wiki/fly"
##response = requests.get(url)
##source_code = response.content
##soup = BeautifulSoup(source_code, "html.parser")
##textOnly = soup.get_text()
##separatorEnd = "Retrieved from \"https://en.wiktionary.org"
##rest = textOnly.split(separatorEnd, 1)[0]
##separatorStart = "skins.vector.js\"]);});"
##rest = rest.split(separatorStart, 1)[1]
url = "https://en.wiktionary.org/wiki/fly"
response = requests.get(url)
source_code = response.content.decode()
# Remove bottom of the page by the perf comment, everything including it and after is useless for us
separatorEnd = "<!-- \n"
rest = source_code.split(separatorEnd, 1)[0]
# Remove everything before and including the contents tab, it's useless for us. (first <h2> is contents so we skip it)
separatorStart = "<h2>"
rest = rest.split(separatorStart, 2)[2]
# 'rest' is now a block of HTML code per-language
rest = rest.split(separatorStart, 999)
# 'rest' is now an array of every <h2> block
for x in range(len(rest)):
# Throw the current block in loop into BS4 parser
soup = BeautifulSoup(rest[x], "html.parser")
# Get Language of the block
blockLanguage = soup.find(class_="mw-headline").get_text()
print("Language is: "+blockLanguage)
if blockLanguage == "Norwegian":
print("WTF Block language is 'NORWEGIAN' - TEST FAIL, EXITING")
exit(123)
# if blockLanguage == "Norwegian Bokmål":
if blockLanguage == "Swedish":
print("Matches language NB")
# print(rest[x])
# print(soup.get_text())
awoo = soup.findAll(id="Verb*")
print(awoo)
if blockLanguage == "Norwegian Nynorsk":
print("Matches language NN")
# print(rest[x])
# print(rest[x])
exit(0)
soup = BeautifulSoup(source_code, "html.parser")
print(soup.find_all('h2')[0].get_text())
textOnly = soup.get_text()
separatorEnd = "Retrieved from \"https://en.wiktionary.org"
rest = textOnly.split(separatorEnd, 1)[0]
separatorStart = "skins.vector.js\"]);});"
rest = rest.split(separatorStart, 1)[1]
##print(rest)
# print(soup.prettify())