nlpblogs commited on
Commit
8732927
·
verified ·
1 Parent(s): aa2ff25

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -14
app.py CHANGED
@@ -121,20 +121,23 @@ if st.button("Sentiment Analysis", type="secondary"):
121
  placeholder.text("Scrolling complete.")
122
  progress_bar.empty()
123
 
124
- data = []
125
- try:
126
- wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#contents #contents")))
127
- comments = driver.find_elements(By.CSS_SELECTOR, "#content #content-text")
128
- user_id = 1
129
- for comment in comments:
130
- timestamp_elements = comment.find_elements(By.CSS_SELECTOR, 'div.author-info > time')
131
- timestamp = timestamp_elements[0].get_attribute('datetime') if timestamp_elements else None
132
- data.append({"User ID": user_id, "Comment": comment.text, "comment_date": timestamp})
133
- user_id += 1
134
- data = [dict(t) for t in {tuple(d.items()) for d in data}]
135
-
136
- except Exception as e:
137
- st.error(f"Exception during comment extraction: {e}")
 
 
 
138
  driver.quit()
139
  df = pd.DataFrame(data, columns=["User ID", "Comment", "comment_date"])
140
  st.dataframe(df)
 
121
  placeholder.text("Scrolling complete.")
122
  progress_bar.empty()
123
 
124
+ try:
125
+ wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#contents #contents")))
126
+ comments = driver.find_elements(By.CSS_SELECTOR, "#content #content-text")
127
+ user_id = 1
128
+ for comment in comments:
129
+ timestamp = None
130
+ try:
131
+ timestamp_element = comment.find_element(By.XPATH, './ancestor::ytd-comment-renderer//yt-formatted-string[@class="published-time-text style-scope ytd-comment-renderer"]')
132
+ timestamp = timestamp_element.text
133
+ except Exception as e:
134
+ print(f"Timestamp not found for comment: {comment.text}. Error: {e}")
135
+ data.append({"User ID": user_id, "Comment": comment.text, "comment_date": timestamp})
136
+ user_id += 1
137
+ data = [dict(t) for t in {tuple(d.items()) for d in data}]
138
+ except Exception as e:
139
+ st.error(f"Exception during comment extraction: {e}")
140
+
141
  driver.quit()
142
  df = pd.DataFrame(data, columns=["User ID", "Comment", "comment_date"])
143
  st.dataframe(df)