generated from wanghaisheng/mood-emotion-charts
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetbrowser.py
98 lines (83 loc) · 3.08 KB
/
getbrowser.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
from dotenv import load_dotenv
from DrissionPage import Chromium, ChromiumOptions
import os
import json
import platform
import subprocess
from pathlib import Path
def find_chrome_path():
"""Find Chrome browser path based on operating system"""
system = platform.system()
if system == "Linux":
# Common Linux Chrome paths
chrome_paths = [
"/usr/bin/google-chrome",
"/usr/bin/google-chrome-stable",
"/usr/bin/chromium-browser",
"/usr/bin/chromium",
"/snap/bin/chromium",
"/snap/chromium/current/usr/lib/chromium-browser/chrome",
]
# Try to find Chrome using 'which' command
try:
chrome_path = subprocess.check_output(
["which", "google-chrome"],
stderr=subprocess.STDOUT
).decode().strip()
chrome_paths.insert(0, chrome_path)
except subprocess.CalledProcessError:
pass
# Check each path
for path in chrome_paths:
if os.path.exists(path):
print(f"Found Chrome at: {path}")
return path
elif system == "Darwin": # macOS
chrome_paths = [
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
"~/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
]
for path in chrome_paths:
path = os.path.expanduser(path)
if os.path.exists(path):
return path
elif system == "Windows":
chrome_paths = [
r"C:\Program Files\Google\Chrome\Application\chrome.exe",
r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
r"%LOCALAPPDATA%\Google\Chrome\Application\chrome.exe",
r"%PROGRAMFILES%\Google\Chrome\Application\chrome.exe",
r"%PROGRAMFILES(X86)%\Google\Chrome\Application\chrome.exe"
]
for path in chrome_paths:
path = os.path.expandvars(path)
if os.path.exists(path):
return path
print("Chrome not found in common locations")
return None
def setup_chrome():
"""Setup Chrome with appropriate configurations"""
chrome_path = find_chrome_path()
if not chrome_path:
raise Exception("Chrome browser not found. Please install Chrome.")
co = ChromiumOptions()
co.set_browser_path(chrome_path)
co.set_argument('--no-sandbox') # 无沙盒模式
co.headless() # 无头模式
return Chromium(co)
def main():
print("System Information:")
print(f"Operating System: {platform.system()}")
print(f"OS Version: {platform.version()}")
print(f"Machine: {platform.machine()}")
print("\nStarting search volume retrieval...")
try:
browser = setup_chrome()
keywords = get_keywords()
except Exception as e:
print(f"Error in main execution: {str(e)}")
finally:
if 'browser' in locals():
browser.quit()
if __name__ == "__main__":
main()