无法使用xpath获取youtube视频的时长 [英] Failing to get duration of youtube video using xpath
问题描述
我想写点东西,让我返回youtube链接的视频时长.因此,我找到了requests
和lxml
,并按照此开始a>指南.
I wanted to write something that would return me the video duration of a youtube link. So I found requests
and lxml
and started out following this guide.
这是设置:
import requests
from lxml import html
url = 'https://www.youtube.com/watch?v=EN8fNb6uhns'
page = requests.get(url)
tree = html.fromstring(page.content)
然后我尝试使用xpath来获取持续时间,但是它不起作用.尝试获取持续时间:
Then I try and use xpath to get the duration, but it doesn't work. Trying to get the duration:
tree.xpath('//span[@class="ytp-time-duration"]/text()')
返回一个空列表.但是,当我尝试通过以下方式获得标题(作为测试)时:
returns an empty list. But when I try and get the title (as a test) with:
tree.xpath('//h1[@class="watch-title-container"]/span/text()')
有效.当我使用校对来复制duration元素的xpath时,什么也没有返回:
it works. When I use inspect to copy the xpath of the duration element nothing is returned:
tree.xpath('/html/body/div[2]/div[4]/div/div[4]/div[2]/div[2]/div/div[24]/div[2]/div[1]/div/span[3]')
当我为标题做同样的事情时,它又可以工作了.
When I do the same for the title it works again.
这是怎么回事?
推荐答案
span[@class="ytp-time-duration"]
此span
标记是由JavaScript生成的,它不会由requests
返回,requests
仅返回HTML代码
this span
tag is generated by JavaScript, and it will not returned by requests
, requests
just return the HTML code
这篇关于无法使用xpath获取youtube视频的时长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!