Collect YouTube video views and upload date using xpath

Viewed 35

I am sorry I am a newbie to python it would be helpful if some1 know how to resolve it.

I got a strange result when I tried to get the video views and the upload date of a list of videos from YouTube.

0 : https://www.youtube.com/watch?v=XXXXXXXXXXX <selenium.webdriver.remote.webelement.WebElement (session="XXXXXXXXXXX", element="XXXXXXXXXXX")> <selenium.webdriver.remote.webelement.WebElement (session="XXXXXXXXXXX", element="XXXXXXXXXXX")> <selenium.webdriver.remote.webelement.WebElement (session="XXXXXXXXXXX", element="XXXXXXXXXXX")>

Here is my code:

import requests
from bs4 import BeautifulSoup
from selenium import webdriver

driver = webdriver.Chrome(executable_path='C:/XXX/chromedriver.exe')

dataset = pd.read_csv(r"C:\XXXX.csv", skiprows=0)
dataset.head()

for index, row in dataset[0:1].iterrows():
    try:
        links = str(dataset.loc[index,'youtube_link'])
        driver.get(links)
        time.sleep(3)
        print(index, ":", links, driver.find_element_by_xpath("//meta[@itemprop='name']"),driver.find_element_by_xpath("//meta[@itemprop='uploadDate']"),driver.find_element_by_xpath("//meta[@itemprop='interactionCount']"))
    except: 
        pass

1 Answers

driver.find_element_by_xpath() will return the webelement and which is returning as expected.

If you need to return text of the element then you need to add .text with the element.

So code will be

print(index, ":", links, driver.find_element_by_xpath("//meta[@itemprop='name']").text,driver.find_element_by_xpath("//meta[@itemprop='uploadDate']").text,driver.find_element_by_xpath("//meta[@itemprop='interactionCount']").text)

Update:

You need to fetch the get_attribute("content") attribute of the element.

print(index, ":", links, driver.find_element_by_xpath("//meta[@itemprop='name']").text,driver.find_element_by_xpath("//meta[@itemprop='uploadDate']").get_attribute("content"),driver.find_element_by_xpath("//meta[@itemprop='interactionCount']").get_attribute("content"))
Related