View difference between Paste ID: Xzxzvd9y and U2fsJe2q
SHOW: | | - or go back to the newest paste.
1
import multiprocessing
2
import os
3
from concurrent.futures import ProcessPoolExecutor
4
5
from playwright.sync_api import sync_playwright
6
from playwright_stealth import stealth_sync
7
8
9
def find_res(city, shared_list):
10
    res = []
11
    try:
12
        with sync_playwright() as pw:
13
            proxy = {
14
                'server': os.getenv('PROXY_SERVER'),
15
                'username': os.getenv('PROXY_USERNAME'),
16
                'password': os.getenv('PROXY_PASSWORD')
17
            }
18
            browser = pw.firefox.launch(headless=True, slow_mo=200)
19
            context = browser.new_context(proxy=proxy)
20
            stealth_sync(context)
21
            page = context.new_page()
22
            resp = page.goto(f"https://site.com/{city}")
23
            if resp.status == 200:
24
                shared_list.append(resp.url)
25
                res.append(resp.text())
26
27
    except Exception as e:
28
        print(f"Exception: {e}")
29
        return []
30
31
    return res
32
33
34
def process_city(args):
35
    city, shared_list = args
36
    return find_res(city, shared_list)
37
38
39
def main():
40
    total_res = []
41
    cities = ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix', 'Philadelphia', 'San Antonio', 'San Diego',]
42
    with multiprocessing.Manager() as manager:
43
        shared_list = manager.list()
44
        with ProcessPoolExecutor(max_workers=10) as executor:
45
            for res in executor.map(process_city, [(city, shared_list) for city in cities]):
46
                total_res.append(res)
47
48
    print(total_res)
49
50
51
if __name__ == '__main__':
52
    main()
53