gpt4 book ai didi

python-3.x - 无法理解语法href =\“\

转载 作者:行者123 更新时间:2023-12-03 05:54:47 26 4
gpt4 key购买 nike

我正在尝试编写一个简单的python脚本,以使用youtube-dl搜索和下载youtube视频。我遇到了以下搜索视频ID的code。我无法理解以下内容:

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

youtube视频链接是这样的: https://www.youtube.com/watch?v=MJGkm0UwNRk
href = \“\的使用是否意味着跳过 https://www.youtube.com部分并移至/ watch?v = <11digit 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])

最佳答案

您应该检查regular expression operations

这是regex101的解释:

"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

关于python-3.x - 无法理解语法href =\“\,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47949677/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com