-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtwitch_scraper.py
More file actions
56 lines (43 loc) · 1.99 KB
/
twitch_scraper.py
File metadata and controls
56 lines (43 loc) · 1.99 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
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import random
import time
from bs4 import BeautifulSoup
from fake_useragent import UserAgent
def get_clip_data(num_clips, url):
# Configurate UserAgent
ua = UserAgent(platforms='pc') # Create a UserAgent instance
random_user_agent = ua.random # Get a random user-agent string
# Configure Chrome options
chrome_options = Options()
chrome_options.add_argument("--headless=new")
#chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument(f"user-agent={random_user_agent}")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("window-size=960,1080")
chrome_options.add_argument("log-level=1")
# Create a new instance of the Chrome driver
driver = webdriver.Chrome(options=chrome_options)
# Setting up the scrape
driver.get(url) # Navigate to the URL
time.sleep(random.uniform(4.0, 6.5)) # Wait for the page to load (adjust the delay as needed)
html_content = driver.page_source # Get the page source after JavaScript rendering
soup = BeautifulSoup(html_content, "html.parser") # Parse the HTML content using BeautifulSoup
# Scraping the desired number of clips, saving in a list of dictionarys
clips = soup.select("div[data-a-target^='clips-card-']") # Find all the clip elements
clip_data = []
for clip in clips[:num_clips]:
# Find the link tag and extract the href attribute
link_tag = clip.select_one("a.ScCoreLink-sc-16kq0mq-0.eFqEFL.tw-link[data-a-target='preview-card-image-link']")
if link_tag:
link_href = "https://www.twitch.tv" + link_tag.get("href")
else:
link_href = ""
clip_info = link_href
# Add the clip information to the list
clip_data.append(clip_info)
# Introduce a random delay to simulate human browsing behavior
time.sleep(random.uniform(1.5, 3.5))
# Exit browser
driver.quit()
return clip_data