2017-12-23 6 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 
관련 문제