Search
Search titles only
By:
Search titles only
By:
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Forums
New posts
All threads
Latest threads
New posts
Trending threads
Trending
Search forums
What's new
New posts
New ads
New profile posts
Latest activity
Free Ads
Latest reviews
Search ads
Members
Current visitors
New profile posts
Search profile posts
Contact us
Latest ads
එක පැකේජ් එකයි මාසෙටම Unlimited Internet. තාමත් DATA CARD දාන්න සල්ලි වියදම් කරනවද? අඩුම මිලට අපෙන්.
sayuru bandara
Updated:
Tuesday at 12:30 PM
Ad icon
ඉන්ටර්නෙට් එකෙන් හරියටම සල්ලි හොයන්න සහ Success වෙන්න කැමතිද? 🚀 (E-Money & Success Stories)
siri sumana
Updated:
Saturday at 11:44 PM
Gemini AI PRO 18 months Offer
Hawaka
Updated:
May 27, 2026
Ad icon
koko account
DasunEranga
Updated:
May 27, 2026
Ad icon
koko account
DasunEranga
Updated:
May 27, 2026
Electronics
Vehicles
Property
Search
Reply to thread
Forums
Entertainment
New Artist or Actor
kalathma - Python
Get the App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="VimangaLK" data-source="post: 30656699" data-attributes="member: 587054"><p>[CODE=python]from selenium import webdriver</p><p>from selenium.webdriver.common.by import By</p><p>from selenium.webdriver.chrome.options import Options</p><p>from selenium.webdriver.support.ui import WebDriverWait</p><p>from selenium.webdriver.support import expected_conditions as EC</p><p>from selenium.common.exceptions import TimeoutException, NoSuchElementException</p><p>import time</p><p>import csv</p><p># TikTok video URL</p><p>url = "https://www.tiktok.com/@rizzcado/photo/7491129701956652306"</p><p># Configure Chrome options for headless browsing</p><p>chrome_options = Options()</p><p># Comment out headless mode for debugging if needed</p><p># chrome_options.add_argument("--headless")</p><p>chrome_options.add_argument("--disable-gpu")</p><p>chrome_options.add_argument("--mute-audio")</p><p>chrome_options.add_argument("--window-size=1920,1080") # Set window size</p><p>chrome_options.add_argument("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36")</p><p># Initialize the Chrome driver</p><p>driver = webdriver.Chrome(options=chrome_options)</p><p># Set page load timeout</p><p>driver.set_page_load_timeout(30)</p><p># Navigate to the URL</p><p>print(f"Opening TikTok URL: {url}")</p><p>driver.get(url)</p><p># Wait for page to load properly</p><p>time.sleep(5)</p><p># Function to scroll down to load more comments</p><p>def scroll_to_load_comments(driver, max_scrolls=15):</p><p>print("Starting to scroll to load comments...")</p><p>last_height = driver.execute_script("return document.body.scrollHeight")</p><p>scroll_count = 0</p><p></p><p>while scroll_count < max_scrolls:</p><p># Scroll down to bottom</p><p>driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")</p><p></p><p># Wait to load page</p><p>time.sleep(3)</p><p></p><p># Calculate new scroll height and compare with last scroll height</p><p>new_height = driver.execute_script("return document.body.scrollHeight")</p><p>if new_height == last_height:</p><p># Try clicking "load more" if it exists</p><p>try:</p><p>load_buttons = driver.find_elements(By.XPATH, "//*[contains(text(), 'Load more') or contains(text(), 'View more')]")</p><p>if load_buttons:</p><p>for button in load_buttons:</p><p>try:</p><p>print("Clicking load more button...")</p><p>driver.execute_script("arguments[0].click();", button)</p><p>time.sleep(2)</p><p>except:</p><p>pass</p><p>else:</p><p># If we scrolled a few times and no new content, break</p><p>if scroll_count > 3:</p><p>break</p><p>except:</p><p>if scroll_count > 3:</p><p>break</p><p></p><p>last_height = new_height</p><p>scroll_count += 1</p><p>print(f"Completed scroll {scroll_count}/{max_scrolls}")</p><p># Try to handle cookie consent if it appears</p><p>try:</p><p>cookie_buttons = driver.find_elements(By.XPATH, "//*[contains(text(), 'Accept') or contains(text(), 'Accept all')]")</p><p>if cookie_buttons:</p><p>for button in cookie_buttons:</p><p>try:</p><p>button.click()</p><p>print("Clicked cookie consent button")</p><p>time.sleep(1)</p><p>except:</p><p>pass</p><p>except:</p><p>pass</p><p># Scroll to load comments</p><p>scroll_to_load_comments(driver)</p><p># Try different CSS selectors for comments</p><p>possible_comment_selectors = [</p><p>".tiktok-x6o8yi-DivCommentItem",</p><p>"[data-e2e='comment-item']",</p><p>".comment-item",</p><p>".tiktok-comment-item",</p><p>"div[class*='CommentItem']"</p><p>]</p><p>comments = []</p><p>used_selector = None</p><p>for selector in possible_comment_selectors:</p><p>print(f"Trying to find comments with selector: {selector}")</p><p>comment_elements = driver.find_elements(By.CSS_SELECTOR, selector)</p><p></p><p>if comment_elements:</p><p>used_selector = selector</p><p>print(f"Found {len(comment_elements)} comments using selector: {selector}")</p><p>break</p><p>if used_selector:</p><p>comment_elements = driver.find_elements(By.CSS_SELECTOR, used_selector)</p><p></p><p>for element in comment_elements:</p><p>try:</p><p># Try different selectors for username and comment text</p><p>username_selectors = [</p><p>'a[data-e2e="comment-username"]',</p><p>'span[data-e2e="comment-username"]',</p><p>'.nickname',</p><p>'a.user-nickname',</p><p>'div[class*="Author"] span'</p><p>]</p><p></p><p>comment_selectors = [</p><p>'p[data-e2e="comment-level"]',</p><p>'span[data-e2e="comment-text"]',</p><p>'.comment-text',</p><p>'div[class*="CommentText"]'</p><p>]</p><p></p><p>user_name = None</p><p>for username_selector in username_selectors:</p><p>try:</p><p>user_name_element = element.find_element(By.CSS_SELECTOR, username_selector)</p><p>user_name = user_name_element.text.strip()</p><p>if user_name:</p><p>break</p><p>except:</p><p>continue</p><p></p><p>comment_text = None</p><p>for comment_selector in comment_selectors:</p><p>try:</p><p>comment_text_element = element.find_element(By.CSS_SELECTOR, comment_selector)</p><p>comment_text = comment_text_element.text.strip()</p><p>if comment_text:</p><p>break</p><p>except:</p><p>continue</p><p></p><p>if user_name and comment_text:</p><p>comments.append([user_name, comment_text])</p><p>print(f"Extracted comment from {user_name}: {comment_text[:30]}...")</p><p>except Exception as e:</p><p>print(f"Error extracting comment: {e}")</p><p>else:</p><p># If no comments found with CSS selectors, try XPath as a fallback</p><p>print("No comments found with CSS selectors, trying XPath...")</p><p>try:</p><p>comment_elements = driver.find_elements(By.XPATH, "//div[contains(@class, 'Comment') or contains(@class, 'comment')]")</p><p>print(f"Found {len(comment_elements)} comments using XPath")</p><p></p><p>for element in comment_elements:</p><p>try:</p><p>full_text = element.text</p><p># Simple text processing to extract username and comment</p><p>if ":" in full_text:</p><p>parts = full_text.split(":", 1)</p><p>user_name = parts[0].strip()</p><p>comment_text = parts[1].strip()</p><p>comments.append([user_name, comment_text])</p><p>print(f"Extracted comment via text processing: {user_name}: {comment_text[:30]}...")</p><p>except Exception as e:</p><p>print(f"Error extracting comment with XPath: {e}")</p><p>except Exception as e:</p><p>print(f"Error with XPath approach: {e}")</p><p># Take a screenshot for debugging purposes</p><p>screenshot_file = "tiktok_screenshot.png"</p><p>driver.save_screenshot(screenshot_file)</p><p>print(f"Saved screenshot to {screenshot_file} for debugging")</p><p># Print summary</p><p>print(f"Total comments extracted: {len(comments)}")</p><p># Close the driver</p><p>driver.quit()</p><p># Write comments to CSV file</p><p>csv_file = "tiktok_comments.csv"</p><p>with open(csv_file, mode='w', newline='', encoding='utf-8') as file:</p><p>writer = csv.writer(file)</p><p>writer.writerow(["Username", "Comment"]) # Write header</p><p>writer.writerows(comments) # Write comments</p><p>print(f"Comments saved to {csv_file}")[/CODE]</p></blockquote><p></p>
[QUOTE="VimangaLK, post: 30656699, member: 587054"] [CODE=python]from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException, NoSuchElementException import time import csv # TikTok video URL url = "https://www.tiktok.com/@rizzcado/photo/7491129701956652306" # Configure Chrome options for headless browsing chrome_options = Options() # Comment out headless mode for debugging if needed # chrome_options.add_argument("--headless") chrome_options.add_argument("--disable-gpu") chrome_options.add_argument("--mute-audio") chrome_options.add_argument("--window-size=1920,1080") # Set window size chrome_options.add_argument("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36") # Initialize the Chrome driver driver = webdriver.Chrome(options=chrome_options) # Set page load timeout driver.set_page_load_timeout(30) # Navigate to the URL print(f"Opening TikTok URL: {url}") driver.get(url) # Wait for page to load properly time.sleep(5) # Function to scroll down to load more comments def scroll_to_load_comments(driver, max_scrolls=15): print("Starting to scroll to load comments...") last_height = driver.execute_script("return document.body.scrollHeight") scroll_count = 0 while scroll_count < max_scrolls: # Scroll down to bottom driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") # Wait to load page time.sleep(3) # Calculate new scroll height and compare with last scroll height new_height = driver.execute_script("return document.body.scrollHeight") if new_height == last_height: # Try clicking "load more" if it exists try: load_buttons = driver.find_elements(By.XPATH, "//*[contains(text(), 'Load more') or contains(text(), 'View more')]") if load_buttons: for button in load_buttons: try: print("Clicking load more button...") driver.execute_script("arguments[0].click();", button) time.sleep(2) except: pass else: # If we scrolled a few times and no new content, break if scroll_count > 3: break except: if scroll_count > 3: break last_height = new_height scroll_count += 1 print(f"Completed scroll {scroll_count}/{max_scrolls}") # Try to handle cookie consent if it appears try: cookie_buttons = driver.find_elements(By.XPATH, "//*[contains(text(), 'Accept') or contains(text(), 'Accept all')]") if cookie_buttons: for button in cookie_buttons: try: button.click() print("Clicked cookie consent button") time.sleep(1) except: pass except: pass # Scroll to load comments scroll_to_load_comments(driver) # Try different CSS selectors for comments possible_comment_selectors = [ ".tiktok-x6o8yi-DivCommentItem", "[data-e2e='comment-item']", ".comment-item", ".tiktok-comment-item", "div[class*='CommentItem']" ] comments = [] used_selector = None for selector in possible_comment_selectors: print(f"Trying to find comments with selector: {selector}") comment_elements = driver.find_elements(By.CSS_SELECTOR, selector) if comment_elements: used_selector = selector print(f"Found {len(comment_elements)} comments using selector: {selector}") break if used_selector: comment_elements = driver.find_elements(By.CSS_SELECTOR, used_selector) for element in comment_elements: try: # Try different selectors for username and comment text username_selectors = [ 'a[data-e2e="comment-username"]', 'span[data-e2e="comment-username"]', '.nickname', 'a.user-nickname', 'div[class*="Author"] span' ] comment_selectors = [ 'p[data-e2e="comment-level"]', 'span[data-e2e="comment-text"]', '.comment-text', 'div[class*="CommentText"]' ] user_name = None for username_selector in username_selectors: try: user_name_element = element.find_element(By.CSS_SELECTOR, username_selector) user_name = user_name_element.text.strip() if user_name: break except: continue comment_text = None for comment_selector in comment_selectors: try: comment_text_element = element.find_element(By.CSS_SELECTOR, comment_selector) comment_text = comment_text_element.text.strip() if comment_text: break except: continue if user_name and comment_text: comments.append([user_name, comment_text]) print(f"Extracted comment from {user_name}: {comment_text[:30]}...") except Exception as e: print(f"Error extracting comment: {e}") else: # If no comments found with CSS selectors, try XPath as a fallback print("No comments found with CSS selectors, trying XPath...") try: comment_elements = driver.find_elements(By.XPATH, "//div[contains(@class, 'Comment') or contains(@class, 'comment')]") print(f"Found {len(comment_elements)} comments using XPath") for element in comment_elements: try: full_text = element.text # Simple text processing to extract username and comment if ":" in full_text: parts = full_text.split(":", 1) user_name = parts[0].strip() comment_text = parts[1].strip() comments.append([user_name, comment_text]) print(f"Extracted comment via text processing: {user_name}: {comment_text[:30]}...") except Exception as e: print(f"Error extracting comment with XPath: {e}") except Exception as e: print(f"Error with XPath approach: {e}") # Take a screenshot for debugging purposes screenshot_file = "tiktok_screenshot.png" driver.save_screenshot(screenshot_file) print(f"Saved screenshot to {screenshot_file} for debugging") # Print summary print(f"Total comments extracted: {len(comments)}") # Close the driver driver.quit() # Write comments to CSV file csv_file = "tiktok_comments.csv" with open(csv_file, mode='w', newline='', encoding='utf-8') as file: writer = csv.writer(file) writer.writerow(["Username", "Comment"]) # Write header writer.writerows(comments) # Write comments print(f"Comments saved to {csv_file}")[/CODE] [/QUOTE]
Insert quotes…
Verification
Haya warak paha keeyada? (haya wadi kireema paha)
Post reply
Top
Bottom