如何从HTMLYouTube页面使用正则表达式在匹配组之间仅获取文本
我用尖叫蛙来抓取youtube视频关键词。 我知道该软件会显示一个标签,准确地捕捉到这些元信息,但它只显示160个字符,因此带有大量关键字的视频不会显示在那里 无论如何,我也尝试通过软件上的自定义提取功能使用CSS选择器和Xpath,但没有得到任何结果 我能想到的最后一件事是在自定义提取中使用正则表达式直接从html页面捕获和提取关键字 这是出现关键字的部分:如何从HTMLYouTube页面使用正则表达式在匹配组之间仅获取文本,html,xpath,youtube,web-scraping,keyword-search,Html,Xpath,Youtube,Web Scraping,Keyword Search,我用尖叫蛙来抓取youtube视频关键词。 我知道该软件会显示一个标签,准确地捕捉到这些元信息,但它只显示160个字符,因此带有大量关键字的视频不会显示在那里 无论如何,我也尝试通过软件上的自定义提取功能使用CSS选择器和Xpath,但没有得到任何结果 我能想到的最后一件事是在自定义提取中使用正则表达式直接从html页面捕获和提取关键字 这是出现关键字的部分: <meta property="og:video:tag" content="lanshow"> <m
<meta property="og:video:tag" content="lanshow">
<meta property="og:video:tag" content="lanshow ep04">
<meta property="og:video:tag" content="lanshow episodio 4">
<meta property="og:video:tag" content="lanshow 4">
<meta property="og:video:tag" content="directo unboxme">
<meta property="og:video:tag" content="directo tecnologia">
<meta property="og:video:tag" content="directo hardware">
<meta property="og:video:tag" content="directo preguntas y respuestas">
<meta property="og:video:tag" content="preguntas y respuestas unboxme">
它们也一个接一个地出现在下面,如下所示:
“关键词”:“lanshow,lanshow ep04,lanshow情节4,lanshow 4,directo Unbxme,directo tecnologia,directo hardware,directo preguntas y respuestas,preguntas y respuestas Unbxme,“c”:“WEB”,“player_response”:“{”视频详细信息“:{”缩略图“:{”缩略图
有没有办法只捕获关键字,使用正则表达式、捕获组或类似的东西
我尝试了不同的正则表达式组合,但我得到了整个文本,甚至html的所有剩余文本都显示在提取中
这仅表示第一个关键字:
视频:标记“内容=.*?>
我还尝试了另一个正则表达式,它在第一个关键字之后提取整个html文本。我需要找到一种方法,告诉提取器查找前后分隔符,并在提取时忽略它们,以仅获取介于两者之间的内容(实际关键字)
这是before分隔符:
这是后面的分隔符:
“>
有办法吗
谢谢。XPath表达式
//meta[@property='og:video:tag']/attribute::content
应该会为您提供所有相关信息
以下是Python代码片段,因为我不熟悉尖叫蛙:
import requests
import lxml.html
doc = lxml.html.parse('yt.html')
meta_tags = doc.xpath("//meta[@property='og:video:tag']/attribute::content")
for content in meta_tags:
print content
或者(使用正则表达式解析HTML,可能会导致不需要的结果):我能想到的最简单的正则表达式与HTML元标记匹配。在您选择的编程语言或工具中,特定的字符和标志可能会有所不同,但这应能在许多情况下起作用:
<meta property="og:video:tag" content="(.+?)">
用你尝试过的一切更新你的问题
import re
import requests
match_metas = re.compile('<meta property="og:video:tag" content="(.+?)">')
result = requests.get('https://www.youtube.com/watch?v=HHMdrAhVbLo')
print match_metas.findall(result.content)
['unboxing en directo', 'unboxing mionix color', 'rx 580', 'talk show', 'lanshow', 'lanshow ep04', 'lanshow episodio 4', 'lanshow 4', 'directo unboxme', 'directo tecnologia', 'directo hardware', 'directo preguntas y respuestas', 'preguntas y respuestas unboxme']