Scraping YouTube – can't scrape views successfully with Selenium
Question:
I’m doing a scraping project for Uni and I’m having a real hard time with some YouTube features. The main one I’m having trouble with is the views.
Inspecting the code on the actual page, I found:
<span dir="auto" class="style-scope yt-formatted-string bold" style-target="bold">10,504 views</span>
So using XPATH, here’s what I tried:
views = driver.find_elements(By.XPATH, "//yt-formatted-string[@class='style-scope ytd-watch-metadata']")
for e in views:
print(e.text)
The results are quite strange to me (sorry, still a newbie). Here’s what I got:
Assemblea Confindustria 2019 - Video di apertura
10K views 3 years ago
10K views 3 years ago
What am I doing wrong? I don’t get it, and I’ve tried to read countless tutorials or manuals, to no avail
Answers:
Try this.
//yt-formatted-string[@id='info']/span[1]
I’m doing a scraping project for Uni and I’m having a real hard time with some YouTube features. The main one I’m having trouble with is the views.
Inspecting the code on the actual page, I found:
<span dir="auto" class="style-scope yt-formatted-string bold" style-target="bold">10,504 views</span>
So using XPATH, here’s what I tried:
views = driver.find_elements(By.XPATH, "//yt-formatted-string[@class='style-scope ytd-watch-metadata']")
for e in views:
print(e.text)
The results are quite strange to me (sorry, still a newbie). Here’s what I got:
Assemblea Confindustria 2019 - Video di apertura
10K views 3 years ago
10K views 3 years ago
What am I doing wrong? I don’t get it, and I’ve tried to read countless tutorials or manuals, to no avail
Try this.
//yt-formatted-string[@id='info']/span[1]