Hongli Bu
Hongli Bu

Reputation: 143

how to find correct youtube xpath for python

I want to get information including video title, videoID, video post time, video viewers from videos on Youtube. But I don't know how to find the correct xpath. The following code is suggested by How to extract the title of a youtube video using python

I have tried to use inspect of 'Chrome' and firebug of 'firefox', but all these didn't work for the code (the xpath found is : //*[@id="container"]/h1). The result is all empty list.

import lxml
from lxml import etree

youtube = 
etree.HTML(urllib.request.urlopen("https://www.youtube.com/watch?
v=Tqyu6E_k_cg").read()) 

video_title = youtube.xpath("//span[@id='eow-title']/@title")

print (''.join(video_title))

So how should I find the correct xpaths for videoID, viewers, postdate that can wor?

Upvotes: 0

Views: 1581

Answers (2)

Hongli Bu
Hongli Bu

Reputation: 143

import requests
page = requests.get("https://www.youtube.com/watch?v=Tqyu6E_k_cg")
youtube = html.fromstring(page.text)


video_title = youtube.xpath("//*[@class='watch-title']/text()")
video_count = youtube.xpath("//*[(@class='watch-view-count')]/text()")
video_date = youtube.xpath("//*[(@class='watch-time-text')]/text()")

After modification, this code works!!

Upvotes: -1

Mahesh Karia
Mahesh Karia

Reputation: 2055

video title : //*[@class='watch-title']
view count : //*[(@class='watch-view-count')]
upload date: //*[(@class='watch-time-text')]

Video ID can be fetched from URL.

Upvotes: 2

Related Questions