【问题标题】:Using LXML To Return Title Text使用 LXML 返回标题文本
【发布时间】:2019-10-24 02:19:26
【问题描述】:

我正在做一个学校项目,我正在使用 LXML 和它的 .xpath 函数来尝试获取您可以选择的 youtube 搜索中热门视频的标题。我的问题是,当它遍历前 5 名并返回视频的标题值时,无论我做什么,我似乎都无法返回实际标题。我试图做/text()/string/title/text(),因为我试图获取的文本在标题中,但我所做的一切都只返回一个空白列表[]

这是我的python代码:

from lxml import html
import requests

string = input("Enter what you want to search up on Youtube: \n")
string.replace(" ", "+")
page = requests.get('https://www.youtube.com/results?search_query=' + string)
tree = html.fromstring(page.content)
for x in range(5):
  v = tree.xpath('/html/body/ytd-app/div/ytd-page-manager/ytd-search/div[1]/ytd-two-column-search-results-renderer/div/ytd-section-list-renderer/div[2]/ytd-item-section-renderer/div[3]/ytd-video-renderer[1]/div[1]/div/div[' + str(x) + ']/div/h3/a')
  print(v)

这是我得到的回报:

Enter what you want to search up on Youtube:
rainbow
[]
[]
[]
[]
[]

这是我试图从中提取 TITLE TEXT 的 HTML:

<a id="video-title" class="yt-simple-endpoint style-scope ytd-video-renderer" title="Hide and Seek in Rainbow Six Siege... Let's Go!!" href="/watch?v=g8MM_RS7zmw" aria-label="Hide and Seek in Rainbow Six Siege... Let's Go!! by Get_Flanked 8 hours ago 21 minutes 54,654 views">
                Hide and Seek in Rainbow Six Siege... Let's Go!!
              </a>

这是我第一次创建其中一个,我只是一名学生,所以如果我没有正确格式化某些内容或做错了什么,请放轻松。感谢您的帮助!

【问题讨论】:

  • 需要写string = string.replace(" ", "+"),否则原查询字符串中的空格不会被替换。你能在页面中找到其他元素吗?
  • 如果你检查page.contentpage.text,你实际上不会看到你认为你得到的这些元素。换句话说,您下载的页面与您在浏览器中看到的不同。
  • 谢谢,是的,我能够得到一些“元素(随机内存点)”来返回,但没有比这更远的了,但我想如果我能得到,我应该能够得到我现在的样子在找吗?
  • alecxe 所以你是说我正在下载的页面不包含我想要获取的内容,或者它的树格式不同??
  • 也只是想知道是否没有人回答我的问题只是坐在这里还是大多数人通常都会回答?

标签: python lxml


【解决方案1】:

考虑使用 youtube 数据 api,他们确实有一个 python 库。

否则,如果您想使用某种刮板,您将需要一个可以执行 javascript 的刮板。 requests 只下载html文本文件,不运行javascript。

以硒为例。

import selenium.webdriver

options = selenium.webdriver.FirefoxOptions()
options.add_argument("--headless")

driver = selenium.webdriver.Firefox(firefox_options=options)

driver.get('https://www.youtube.com/results?search_query=montypython')

[x.text for x in driver.find_elements_by_xpath('//*[@id="video-title"]')]
[x.text for x in driver.find_elements_by_id('video-title')]
print(dir(driver))

# how to get html tag attributes for example href
x.get_attribute("href")

>>> [x.get_attribute('title') for x in driver.find_elements_by_id('video-title')]
['Monty Python And The Holy Grail 1975 HD', 'Monty Python and the Holy Grail', "Monty Python's - The Funniest Joke in the World (la blague qui tue)", 'Argument', 'Monty Python - The Black Knight - Tis But A Scratch', 'Monty Python- Cheese Shop', 'Monty Python: The Parrot Sketch & The Lumberjack Song movie versions HQ', 'Biggus Dickus - Monty Python, Life of Brian.', 'Monty Python - Bridge of Death', 'Life of Brian 1979 (sub indo)', 'John Cleese - How To Irritate People 1968', 'Monty Python and The Holy Grail - Black Knight HD', 'Eric Idle - "Always Look On The Bright Side Of Life" - STEREO HQ', 'Monty pythons, Mr creosote, Full version,', 'Monty Python   Ministry of Silly Walks NL', 'Monty Python - careers advice', 'Monty Python and the Holy Grail - Bunny Attack Scene (HD)', 'Monty Python Society For Putting Things On Top of Other Things', 'Monty Python - Constitutional Peasants Scene (HD)']

另见:https://stackoverflow.com/help/how-to-askhttps://stackoverflow.com/tour

只要您的问题表现出一些努力并且清晰,您的问题可能会或可能不会找到答案,这取决于其他人是否能够理解所问的内容并有时间回答。

【讨论】:

    猜你喜欢
    • 2012-03-23
    • 2019-07-08
    • 2019-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 2016-05-08
    • 1970-01-01
    相关资源
    最近更新 更多