如何使用 CLI 从 OK.ru/video 中提取视频 url 和标题
How to extract video urls and titles from OK.ru/video using the CLI
编辑 1:
我想使用 CLI 从“https://ok.ru/video/c1404844”结果中提取视频 urls 和标题。
这是我到目前为止完成的任务:
每个视频相关 URL 的 ERE 模式是:
/video/\d+
视频绝对 URL 看起来像这样:https://ok.ru$videoRelativeURL
我可以使用这个命令来提取视频urls(我使用uniq
因为很多视频ID出现了3次):
$ curl -s https://ok.ru/video/c1404844 | grep -oP "/video/\d+" | uniq | sed "s|^|https://ok.ru|" | head -5
https://ok.ru/video/1896971373228
https://ok.ru/video/1896971438764
https://ok.ru/video/1896971569836
https://ok.ru/video/1896971635372
https://ok.ru/video/1898415590060
然后我尝试用 pup.
提取视频相对 URLs + 标题
编辑 3:我用 video-card_n.ellip
替换了 class 名称 video-card_n ellip
。然而pup
只输出第二个class的属性(video-card_n.ellip
),奇怪:
$ curl -s https://ok.ru/video/c1404844 | pup '.video-card_lk attr{href}, .video-card_n.ellip attr{title}' | head -5
Death.in.Paradise.S02E05.WEBRip.x264-ION10
Death.in.Paradise.S02E02.WEBRip.x264-ION10
Death.in.Paradise.S02E04.WEBRip.x264-ION10
Death.in.Paradise.S02E03.WEBRip.x264-ION10
Death.in.Paradise.S02E06.WEBRip.x264-ION10
它没有用,所以我用这个命令将扩展的 html 转换为 json :
$ curl -s https://ok.ru/video/c1404844 | pup 'json{}' > c1404844.json
现在我想尝试从生成的 json 文件中提取 video-card_n ellip
中的 title
和 video-card_lk
中的 href
jq 工具,但我知道如何使用 jq
就够了。
我想 jq
(或 pup
)输出一个平面文件:url 作为第一列,标题作为第二列。
编辑 2:非常感谢@peak 在 jq
上的帮助!
完成:
$ curl -s https://ok.ru/video/c1404844 | pup 'json{}' | jq -r 'recurse | arrays[] | select(.class == "video-card_lk").href,select(.class == "video-card_n ellip").title' | awk '{videoRelativeURL = [=14=];url="https://ok.ru"gensub("?.*$","",videoRelativeURL); getline title; print url" # "title}' | head
https://ok.ru/video/1898417425068 # Death.in.Paradise.S02E05.WEBRip.x264-ION10
https://ok.ru/video/1898417359532 # Death.in.Paradise.S02E02.WEBRip.x264-ION10
https://ok.ru/video/1898417293996 # Death.in.Paradise.S02E04.WEBRip.x264-ION10
https://ok.ru/video/1898417228460 # Death.in.Paradise.S02E03.WEBRip.x264-ION10
https://ok.ru/video/1898417162924 # Death.in.Paradise.S02E06.WEBRip.x264-ION10
https://ok.ru/video/1898417097388 # Death.in.Paradise.S02E07.WEBRip.x264-ION10
https://ok.ru/video/1898417031852 # Death.in.Paradise.S02E08.WEBRip.x264-ION10
https://ok.ru/video/1898416966316 # Death.in.Paradise.S02E01.WEBRip.x264-ION10
https://ok.ru/video/1898416769708 # Death.in.Paradise.S07E02.The.Stakes.Are.High.WEBRip.x264-ION10
https://ok.ru/video/1898416704172 # Death.in.Paradise.S07E03.Written.in.Murder.WEBRip.x264-ION10
...
在使用pup将顶级页面的HTML转换为JSON后,下面的jq过滤器产生24对,其中前两个显示在"Output" 下面:
[ [ .. | arrays[] | select(.class == "video-card_n ellip").title],
[ .. | arrays[] | select(.class == "video-card_lk").href]]
| transpose
输出
[
[
"Замечательная пара, красивая песня и чудесное исполнение! Золотые голоса!",
"/video/2406311403450?st._aid=VideoState_open_top"
],
[
"#СидимДома",
"/video/1675421949619?st._aid=VideoState_open_top"
],
...
如果您想从 HTML-source 中抓取特定信息,则不需要 5 种不同的工具!请看一下xidel。它可以做到这一切。
$ xidel -s https://ok.ru/video/c1404844 -e '
//div[@data-id]/join(
(
div[@class="video-card_img-w"]/a/resolve-uri(substring-before(@href,"?")),
div[@class="video-card_n-w"]/a
),
" # "
)
'
https://ok.ru/video/1898417425068 # Death.in.Paradise.S02E05.WEBRip.x264-ION10
https://ok.ru/video/1898417359532 # Death.in.Paradise.S02E02.WEBRip.x264-ION10
https://ok.ru/video/1898417293996 # Death.in.Paradise.S02E04.WEBRip.x264-ION10
https://ok.ru/video/1898417228460 # Death.in.Paradise.S02E03.WEBRip.x264-ION10
https://ok.ru/video/1898417162924 # Death.in.Paradise.S02E06.WEBRip.x264-ION10
https://ok.ru/video/1898417097388 # Death.in.Paradise.S02E07.WEBRip.x264-ION10
https://ok.ru/video/1898417031852 # Death.in.Paradise.S02E08.WEBRip.x264-ION10
https://ok.ru/video/1898416966316 # Death.in.Paradise.S02E01.WEBRip.x264-ION10
https://ok.ru/video/1898416769708 # Death.in.Paradise.S07E02.The.Stakes.Are.High.WEBRip.x264-ION10
https://ok.ru/video/1898416704172 # Death.in.Paradise.S07E03.Written.in.Murder.WEBRip.x264-ION10
[...]
编辑 1: 我想使用 CLI 从“https://ok.ru/video/c1404844”结果中提取视频 urls 和标题。
这是我到目前为止完成的任务:
每个视频相关 URL 的 ERE 模式是:
/video/\d+
视频绝对 URL 看起来像这样:https://ok.ru$videoRelativeURL
我可以使用这个命令来提取视频urls(我使用uniq
因为很多视频ID出现了3次):
$ curl -s https://ok.ru/video/c1404844 | grep -oP "/video/\d+" | uniq | sed "s|^|https://ok.ru|" | head -5
https://ok.ru/video/1896971373228
https://ok.ru/video/1896971438764
https://ok.ru/video/1896971569836
https://ok.ru/video/1896971635372
https://ok.ru/video/1898415590060
然后我尝试用 pup.
提取视频相对 URLs + 标题编辑 3:我用 video-card_n.ellip
替换了 class 名称 video-card_n ellip
。然而pup
只输出第二个class的属性(video-card_n.ellip
),奇怪:
$ curl -s https://ok.ru/video/c1404844 | pup '.video-card_lk attr{href}, .video-card_n.ellip attr{title}' | head -5
Death.in.Paradise.S02E05.WEBRip.x264-ION10
Death.in.Paradise.S02E02.WEBRip.x264-ION10
Death.in.Paradise.S02E04.WEBRip.x264-ION10
Death.in.Paradise.S02E03.WEBRip.x264-ION10
Death.in.Paradise.S02E06.WEBRip.x264-ION10
它没有用,所以我用这个命令将扩展的 html 转换为 json :
$ curl -s https://ok.ru/video/c1404844 | pup 'json{}' > c1404844.json
现在我想尝试从生成的 json 文件中提取 video-card_n ellip
中的 title
和 video-card_lk
中的 href
jq 工具,但我知道如何使用 jq
就够了。
我想 jq
(或 pup
)输出一个平面文件:url 作为第一列,标题作为第二列。
编辑 2:非常感谢@peak 在 jq
上的帮助!
完成:
$ curl -s https://ok.ru/video/c1404844 | pup 'json{}' | jq -r 'recurse | arrays[] | select(.class == "video-card_lk").href,select(.class == "video-card_n ellip").title' | awk '{videoRelativeURL = [=14=];url="https://ok.ru"gensub("?.*$","",videoRelativeURL); getline title; print url" # "title}' | head
https://ok.ru/video/1898417425068 # Death.in.Paradise.S02E05.WEBRip.x264-ION10
https://ok.ru/video/1898417359532 # Death.in.Paradise.S02E02.WEBRip.x264-ION10
https://ok.ru/video/1898417293996 # Death.in.Paradise.S02E04.WEBRip.x264-ION10
https://ok.ru/video/1898417228460 # Death.in.Paradise.S02E03.WEBRip.x264-ION10
https://ok.ru/video/1898417162924 # Death.in.Paradise.S02E06.WEBRip.x264-ION10
https://ok.ru/video/1898417097388 # Death.in.Paradise.S02E07.WEBRip.x264-ION10
https://ok.ru/video/1898417031852 # Death.in.Paradise.S02E08.WEBRip.x264-ION10
https://ok.ru/video/1898416966316 # Death.in.Paradise.S02E01.WEBRip.x264-ION10
https://ok.ru/video/1898416769708 # Death.in.Paradise.S07E02.The.Stakes.Are.High.WEBRip.x264-ION10
https://ok.ru/video/1898416704172 # Death.in.Paradise.S07E03.Written.in.Murder.WEBRip.x264-ION10
...
在使用pup将顶级页面的HTML转换为JSON后,下面的jq过滤器产生24对,其中前两个显示在"Output" 下面:
[ [ .. | arrays[] | select(.class == "video-card_n ellip").title],
[ .. | arrays[] | select(.class == "video-card_lk").href]]
| transpose
输出
[
[
"Замечательная пара, красивая песня и чудесное исполнение! Золотые голоса!",
"/video/2406311403450?st._aid=VideoState_open_top"
],
[
"#СидимДома",
"/video/1675421949619?st._aid=VideoState_open_top"
],
...
如果您想从 HTML-source 中抓取特定信息,则不需要 5 种不同的工具!请看一下xidel。它可以做到这一切。
$ xidel -s https://ok.ru/video/c1404844 -e '
//div[@data-id]/join(
(
div[@class="video-card_img-w"]/a/resolve-uri(substring-before(@href,"?")),
div[@class="video-card_n-w"]/a
),
" # "
)
'
https://ok.ru/video/1898417425068 # Death.in.Paradise.S02E05.WEBRip.x264-ION10
https://ok.ru/video/1898417359532 # Death.in.Paradise.S02E02.WEBRip.x264-ION10
https://ok.ru/video/1898417293996 # Death.in.Paradise.S02E04.WEBRip.x264-ION10
https://ok.ru/video/1898417228460 # Death.in.Paradise.S02E03.WEBRip.x264-ION10
https://ok.ru/video/1898417162924 # Death.in.Paradise.S02E06.WEBRip.x264-ION10
https://ok.ru/video/1898417097388 # Death.in.Paradise.S02E07.WEBRip.x264-ION10
https://ok.ru/video/1898417031852 # Death.in.Paradise.S02E08.WEBRip.x264-ION10
https://ok.ru/video/1898416966316 # Death.in.Paradise.S02E01.WEBRip.x264-ION10
https://ok.ru/video/1898416769708 # Death.in.Paradise.S07E02.The.Stakes.Are.High.WEBRip.x264-ION10
https://ok.ru/video/1898416704172 # Death.in.Paradise.S07E03.Written.in.Murder.WEBRip.x264-ION10
[...]