2017-12-23 13 views
0

youtube-dlを使用してYouTube動画を検索してダウンロードする簡単なPythonスクリプトを作成しようとしています。ビデオIDを検索するcodeを見つけました。次の行を理解することができません:構文を理解できませんhref = "

search_results = re.findall(r'href=\"\/watch\?v=(.{11})', html_content.read().decode()) 

ユーチューブビデオのリンクをこのようなものです:?https://www.youtube.com/watch?v=MJGkm0UwNRk "\ = HREFの使用は、\ = https://www.youtube.com部分をスキップし、/観Vに移動することを意味しません< 11桁ID>またはその他のもの。

コード:

import urllib.request 
import urllib.parse 
import re 

query_string = urllib.parse.urlencode({"search_query" : input()}) 
html_content = urllib.request.urlopen("http://www.youtube.com/results?" + query_string) 
search_results = re.findall(r'href=\"\/watch\?v=(.{11})', html_content.read().decode()) 
print("http://www.youtube.com/watch?v=" + search_results[0]) 
+0

@KenWhiteユーチューブリンクは、通常のhttps 'のように書きます:// WWW .youtube.com/watch?v = MJGkm0UwNRk '.href = \ "\は、https://www.youtube.comの部分をスキップすることを意味します。 – rgo

+1

質問を編集して、質問している内容を明確にする必要があります。 –

答えて

1

あなたはregular expression operationsを確認する必要があります。

そして、これはregex101からの説明です:私は、hrefのですが、私は= \ "\原因構文のhrefと混乱しています知っている

"href=\"\/watch\?v=(.{11})"g 

href= matches the characters href= literally (case sensitive) 
\" matches the character " literally (case sensitive) 
\/ matches the character/literally (case sensitive) 
watch matches the characters watch literally (case sensitive) 
\? matches the character ? literally (case sensitive) 
v= matches the characters v= literally (case sensitive) 

1st Capturing Group (.{11}) 
    .{11} matches any character (except for line terminators) 
    {11} Quantifier — Matches exactly 11 times 
関連する問題