gpt4 book ai didi

python - 抓取 YouTube 页面返回空列表

转载 作者:太空宇宙 更新时间:2023-11-03 14:14:10 26 4
gpt4 key购买 nike

我一直在尝试从此 YouTube 页面抓取链接。 。但是, links 变量显示为空。我做错了什么吗? I have attached the code with this question

最佳答案

我认为问题在于您尝试查找链接的方式。当我 curl 与您 curl https://www.youtube.com/results\?search_query\=hello

相同的网址时

我没有得到任何带有这些 css 元素的 a 标签。这似乎基于 header 中设置的 User-Agent 属性。

所以有几个选择:

  1. 更改查找视频链接的方式。

    这就是我的curl 的视频链接标记的样子

    <a href="/watch?v=YQHsXMglC9A" class="yt-uix-tile-link yt-ui-ellipsis yt-ui-ellipsis-2 yt-uix-sessionlink spf-link " data-sessionlink="itct=CFcQ3DAYASITCLfbt4P439gCFQzYfgodkDYKVij0JFIFaGVsbG8" title="Adele - Hello" aria-describedby="description-id-484065" rel="spf-prefetch" dir="ltr">Adele - Hello</a>

    如您所见,这些类在这里不存在。

    但是,您可以在 href 上使用某种正则表达式来查找包含正确格式的内容

    page.find_all("a", {'href': re.compile('/watch?v=[A-Za-z0-9_\-]`)})

    (你必须弄乱正则表达式,它并不完美

  2. 使用Youtube API

    我想说,这将是猜测你想要做什么的首选方法。具体来说search api他们甚至有 python 片段

关于python - 抓取 YouTube 页面返回空列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48310129/

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