-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
88 lines (55 loc) · 1.93 KB
/
main.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
from playwright.sync_api import sync_playwright, Playwright, Page
from selectolax.parser import HTMLParser
import time
import csv
import creds
CSV_file_name = 'Sample'
all_data = []
login_url = ('https://icopify.co/login')
def login(playwright: Playwright):
browser = playwright.chromium.launch()
context = browser.new_context()
page = context.new_page()
page.goto(login_url)
page.fill('input#Email', creds.email)
page.fill('input#password', creds.password)
page.check('input#basic-checkbox')
page.click('button[type=submit]')
print('Succesfully logged in..')
return page
def parse_webiste_info(html: HTMLParser):
website_data = html.css('tbody tr')
for data in website_data:
traffic = data.css('td')[2].text().replace('Monthly Traffic', '').strip()
dr = data.css('td')[3].text().replace('DR', '').strip()
price = data.css('td')[6].css('a')[1].text().strip().replace('$','')
data_dict = {
'Traffic': traffic,
'DR': dr,
'Price': price
}
all_data.append(data_dict)
# print(data_dict)
def run_browser(login: Page):
for x in range(1, 3):
url = f'https://icopify.co/publishers?page={x}'
login.goto(url)
login.is_visible('td.align-middle')
html = login.inner_html('div.table-responsive')
body = HTMLParser(html)
print(f'getting info from page {x}..')
parse_webiste_info(body)
def export_to_csv(products: list):
field_names = ['Traffic', 'DR', 'Price',]
with open(f'{CSV_file_name}.csv', 'w') as f:
writer = csv.DictWriter(f, fieldnames=field_names)
writer.writeheader()
writer.writerows(products)
def main():
with sync_playwright() as playwright:
login_page = login(playwright)
run_browser(login_page)
time.sleep(2)
export_to_csv(all_data)
if __name__ == '__main__':
main()