-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml_parser.py
64 lines (43 loc) · 1.94 KB
/
html_parser.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
from bs4 import BeautifulSoup
import os
import pandas as pd
HTML_FOLDER = 'yeet/'
file_names = os.listdir(HTML_FOLDER)
final_list = [] # TODO: find some other solution other than a global variable
def extract_data(soup):
'''
Function that takes in a single HTML file and returns a DataFrame with the required data
:param soup:
'''
global final_list
for company in soup.div.children:
if company['class'] != ['ad_campaign_search']: # A div in the list is not a company. Something related to ads.
# We'll skip that div
company_name = company.contents[0].div.string
as_code = company.contents[0]['href'][1:]
as_site = "https://www.agencyspotter.com/" + as_code
# company_site = company.contents[1].div.contents[1].a['href']
company_employees = company.contents[2].div.div.contents[1].lstrip()
company_location = company.contents[3].string
company_id = company.contents[3]['data-id']
company_row = [company_id, company_name, company_employees, company_location, as_site, as_code] # single row of data
final_list.append(company_row) # at the end of the loop, we'll have all agencies info from a single HTML page
# final_list = pd.DataFrame()
for file in file_names:
with open(HTML_FOLDER + file) as fp:
soup = BeautifulSoup(fp, "lxml")
print("Parsing " + file)
extract_data(soup)
headings = ['id', 'name', 'employee strength', 'location', 'agency spotter website', 'agency spotter code']
df = pd.DataFrame(final_list, columns=headings)
with open('results.csv', mode='w', newline='') as fn:
df.to_csv(fn, quotechar='"')
'''
#company name
print(main_soup.div.contents[0].contents[0].div.string)
#number of employees, note leading space
print(main_soup.div.contents[0].contents[2].div.div.contents[1])
#company id
print(main_soup.div.contents[0].contents[3]['data-id'])
Sample data:
'''