-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselenium_main.py
More file actions
204 lines (166 loc) · 7.35 KB
/
Copy pathselenium_main.py
File metadata and controls
204 lines (166 loc) · 7.35 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"""
Selenium-based Scraper for Google Issue Tracker
================================================
This script reuses the analysis logic from main.py but uses Selenium
for web scraping to handle potential blocking or SSL issues.
"""
import logging
import time
import sys
import pandas as pd
from tqdm import tqdm
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
from bs4 import BeautifulSoup
# Import helper functions from main.py
# ensure main.py is in the same directory
try:
from main import (
setup_directories, setup_logging, load_csv, remove_duplicates,
detect_url_column, construct_urls_from_ids, apply_categorization,
create_bar_chart, create_pie_chart, create_summary_table, export_results
)
except ImportError:
print("Error: main.py not found or could not import functions.")
sys.exit(1)
import os
def setup_selenium_driver():
"""
Setup Chrome driver with appropriate options.
"""
# Disable SSL verify for webdriver manager
os.environ['WDM_SSL_VERIFY'] = '0'
chrome_options = Options()
chrome_options.add_argument("--headless") # Run properly without UI
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--ignore-certificate-errors")
chrome_options.add_argument("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
# Mute selenium logs
logging.getLogger('selenium').setLevel(logging.WARNING)
logging.getLogger('urllib3').setLevel(logging.WARNING)
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=chrome_options)
return driver
def scrape_with_selenium(df, url_column, logger):
"""
Scrape issues using Selenium.
"""
logger.info("Starting Selenium web scraping...")
descriptions = []
labels_list = []
successful_scrapes = 0
failed_scrapes = 0
driver = None
try:
driver = setup_selenium_driver()
logger.info("[SUCCESS] Selenium driver initialized")
except Exception as e:
logger.error(f"[ERROR] Failed to initialize Selenium driver: {e}")
# Initialize empty columns to prevent downstream errors
df['scraped_description'] = "Scraping Failed"
df['scraped_labels'] = "N/A"
return df, 0, 0
# Progress bar for scraping
for idx, row in tqdm(df.iterrows(), total=len(df), desc="Scraping issues (Selenium)"):
url = row[url_column]
# Validate URL
if pd.isna(url) or not isinstance(url, str):
descriptions.append("Invalid URL")
labels_list.append("N/A")
failed_scrapes += 1
continue
# Add https:// if missing
if not url.startswith('http'):
url = 'https://' + url
try:
driver.get(url)
# Wait briefly for page load (implicit wait handles most, but a small sleep helps with dynamic content render)
# Better to use explicit wait if possible, but structure allows dynamic load
time.sleep(2)
# We can use BeautifulSoup on the driver.page_source for easier parsing
# equivalent to the original logic
soup = BeautifulSoup(driver.page_source, 'lxml')
# Extract issue description (reusing logic from main.py's logic effectively)
description = ""
# Adjust selectors if needed, but the original ones seemed correct for the structure
# main.py used: desc_elements = soup.find_all(['p', 'div'], class_=re.compile('description|issue-desc|comment'))
# We'll try to map that or just use the same BS4 logic on the rendered page
import re
desc_elements = soup.find_all(['p', 'div'], class_=re.compile('description|issue-desc|comment'))
if desc_elements:
description = " ".join([elem.get_text(strip=True) for elem in desc_elements[:3]])
# Extract labels/tags
labels = []
label_elements = soup.find_all(['span', 'div'], class_=re.compile('label|tag|chip'))
if label_elements:
labels = [elem.get_text(strip=True) for elem in label_elements]
# If description is empty, try Selenium placeholders just in case specific JS didn't load for BS4
if not description:
try:
# Fallback generic locator for content
body_text = driver.find_element(By.TAG_NAME, "body").text
description = body_text[:500] if body_text else "No description available"
except:
pass
descriptions.append(description[:500] if description else "No description available")
labels_list.append(', '.join(labels) if labels else "No labels")
successful_scrapes += 1
except Exception as e:
# logger.warning(f"Failed to scrape {url}: {e}") # specific log if needed
descriptions.append("Scraping failed")
labels_list.append("N/A")
failed_scrapes += 1
if driver:
driver.quit()
df['scraped_description'] = descriptions
df['scraped_labels'] = labels_list
logger.info(f"[SUCCESS] Scraping complete: {successful_scrapes} successful, {failed_scrapes} failed")
return df, successful_scrapes, failed_scrapes
def main():
# Setup directories and logging
setup_directories()
logger = setup_logging()
logger.info("Running Selenium-based Scraper Version")
# Load CSV
# Explicitly using the file requested by user
input_file = 'input/issueTracker 2(in) (1).csv'
logger.info(f"Loading input file: {input_file}")
df = load_csv(input_file, logger)
if df is None or len(df) == 0:
logger.error("Failed to load data or empty file.")
return
original_count = len(df)
# Pipeline steps
df = remove_duplicates(df, logger)
duplicates_removed = original_count - len(df)
url_column = detect_url_column(df, logger)
if url_column == 'ID':
df = construct_urls_from_ids(df, 'ID', logger)
url_column = 'url'
elif url_column is None:
logger.error("No URL/ID column found.")
return
# Scrape with Selenium
df, successful, failed = scrape_with_selenium(df, url_column, logger)
# Analysis & Export
df = apply_categorization(df, logger)
create_bar_chart(df, logger)
create_pie_chart(df, logger)
summary_df = create_summary_table(df, logger)
stats = {
'total_issues': original_count,
'successful_scrapes': successful,
'failed_scrapes': failed,
'duplicates_removed': duplicates_removed
}
export_results(df, summary_df, logger, stats)
logger.info("Selenium Run Completed.")
if __name__ == "__main__":
main()