-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrape.py
More file actions
47 lines (39 loc) · 1.75 KB
/
Copy pathscrape.py
File metadata and controls
47 lines (39 loc) · 1.75 KB
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
import argparse
from seleniumbase import SB
def run_scraper():
parser = argparse.ArgumentParser()
parser.add_argument("--url", type=str, default="https://api.ipify.org/")
parser.add_argument("--proxy", type=str, default=None)
parser.add_argument("--headless", type=str, default=None)
parser.add_argument("--incognito", type=str, default=None)
parser.add_argument("--block-images", type=str, default=None)
parser.add_argument("--ad-block", type=str, default=None)
parser.add_argument("--xvfb", type=str, default=None)
args = parser.parse_args()
# uc=True is our only hardcoded default
sb_settings = {"uc": True}
# Helper to only add settings if they are explicitly set
def add_if_not_none(key, value, is_bool=False):
if value is not None and value != "" and value.lower() != "default":
if is_bool:
sb_settings[key] = str(value).lower() == "true"
else:
sb_settings[key] = value
add_if_not_none("proxy", args.proxy)
add_if_not_none("headless", args.headless, is_bool=True)
add_if_not_none("incognito", args.incognito, is_bool=True)
add_if_not_none("block_images", args.block_images, is_bool=True)
add_if_not_none("ad_block", args.ad_block, is_bool=True)
add_if_not_none("xvfb", args.xvfb, is_bool=True)
print(f"--- SeleniumBase Settings: {sb_settings} ---")
with SB(**sb_settings) as sb:
sb.activate_cdp_mode(args.url)
sb.sleep(10)
html = sb.get_page_source()
print("-" * 30)
print(f"URL: {args.url} | Content Length: {len(html)}")
if "ipify" in args.url:
print(f"IP Result: {sb.get_text('body')}")
print("-" * 30)
if __name__ == "__main__":
run_scraper()