-
Notifications
You must be signed in to change notification settings - Fork 1
/
scrape.py
172 lines (152 loc) · 6.79 KB
/
scrape.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
import requests
from bs4 import BeautifulSoup
from readability import Document
import sys
import re
import json
import in_place
import numpy as np
import csv
COUNTRIES_PATH = './Countries.csv'
def get_text(html_text):
doc = Document(html_text)
return doc.summary()
def is_company_type(text, company_type):
return min(p for p in [text.find(f" {company_type} "), text.find(f" {company_type}."), text.find(f" {company_type},"), sys.maxsize ] if p > 0)
def get_legal_forms(file_name):
legal_forms = []
with open(file_name) as json_file:
data = json.load(json_file)
for legal_form in data['data']:
acronym = legal_form['acronym']
if acronym :
acronyms = [l.strip() for l in acronym.split(';') if (len(l.strip()) > 2 and l.strip()[0].isupper())]
legal_forms.extend(acronyms)
return legal_forms
def get_footer_copyright(soup,url):
company_alias = re.findall('https://www.([\w\-]+).(\w+).(\w+)',url)
if company_alias:
company_alias = company_alias[0][0]
print('comp alias:',company_alias)
copyright_text = []
for item in soup.findAll('div', {"id": re.compile('footer')}):
if 'copyright' in item.text.lower():
# print('id',item.text)
pattern = "(?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,4}" + 'copyright' +"(?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,5}"
context = re.search(pattern, item.text.lower())
copyright_text.append(context.group())
for item in soup.findAll('div', {"class": re.compile('footer')}):
if 'copyright' in item.text.lower():
# print('cls', item.text) #.partition(first_legal_form))
pattern = "(?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,4}" + 'copyright' +"(?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,5}"
context = re.search(pattern, item.text.lower())
copyright_text.append(context.group())
if not copyright_text:
unicode_sym = b'\xc2\xa9'
unicode_sym = unicode_sym.decode('utf-8')
copyright_context = re.findall("(?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,4}"+ unicode_sym + "(?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,5}", soup.text)
for text in copyright_context:
if company_alias:
if company_alias in text.lower():
print('Context of copyright symbol: ', text)
return copyright_text
def get_suffixes(file_name):
suffixes =[]
with in_place.InPlace(file_name) as file:
for line in file:
suffixes.append(line.strip())
file.write(line)
return suffixes
def scrape_legal_forms(text_content,legal_forms):
first_legal_form = None
min_position = sys.maxsize
found_legal_forms = []
suffixes = get_suffixes('SuffixesList.txt')
for legal_form in legal_forms:
position_found = is_company_type(text_content, legal_form)
if position_found > 0:
if position_found < min_position:
found_legal_forms.append(legal_form)
first_legal_form = legal_form
min_position = position_found
pattern = "(?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,4}" + first_legal_form# +"(?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,5}"
context = re.search(pattern, text_content)
print(f"Legal form found: '{legal_form}'")
print(f"Context of above legal form: '{context.group()}'")
found_suffixes = []
for suff in suffixes:
if suff in context.group():
print(f"Suffix: '{suff}' found in legal form: '{legal_form}'")
suffix_pattern = "(?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,1}" + suff# +"(?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,5}"
suffix_context = re.search(suffix_pattern, context.group())
print(f"Context of above suffix: '{suffix_context.group()}'")
found_suffixes.append(suffix_context.group())
print('----------')
# print(found_suffixes)
return found_legal_forms, min_position
def get_countries(countries_path):
countries_list =[]
with open(countries_path) as c_file: # pylint: disable=unspecified-encoding
reader = list(csv.DictReader(c_file, delimiter=','))
for row in reader:
countries_list.append(row['Name_EN'])
return countries_list
def scrape_country(text_content):
countries = get_countries(COUNTRIES_PATH)
occurrences = []
for idx, country in enumerate(countries):
count = sum(1 for _ in re.finditer(r'\b%s\b' % re.escape(country),text_content))
occurrences.append(count)
sorted_indexes = np.argsort(np.array(occurrences))
if occurrences[sorted_indexes[-1]] > occurrences[sorted_indexes[-2]] and occurrences[sorted_indexes[-1]] > 0:
print('Country of jurisdiction: ',countries[sorted_indexes[-1]])
def scrape_content(URLs):
for url in URLs:
try:
page = requests.get(url)
soup = BeautifulSoup(page.content, "html.parser")
except Exception as error:
print('Skipping: ', url, ' ERROR: ', str(error))
text_content = get_text(soup.text)
print('Scraping: ', url)
scrape_country(text_content)
copyright_text = get_footer_copyright(soup,url)
if len(copyright_text) > 0:
print('Copyright text: ',copyright_text)
else:
print('Copyright not found')
legal_forms = get_legal_forms('es_legal_forms.json')
found_legal_forms, min_position = scrape_legal_forms(text_content, legal_forms)
if min_position < sys.maxsize:
print('Company type: ', found_legal_forms, url)
else:
print('Legal form not found')
print('==================================================================================')
def get_urls(file_name):
urls = []
with in_place.InPlace(file_name) as file:
for line in file:
if line.strip().startswith('http'):
urls.append(line.strip())
else:
try:
r = requests.get(f"https://{line.strip()}")
if r.url is not None:
if r.url.startswith('https://'):
line = "https://" + line.strip()
else:
line = "http://" + line.strip()
else:
line = "https://" + line.strip()
urls.append(line)
line = line+'\n'
except:
pass
file.write(line)
# print(f"Processed {line}")
return urls
def main():
urls = get_urls('sites.in')
scrape_content(urls)
if __name__ == '__main__':
main()