刮取youtube页面会返回空列表

时间:2018-01-17 21:16:54

标签: python youtube beautifulsoup screen-scraping

我一直试图从这个YouTube页面抓取链接。 。但是,链接变量是空的。我做错了什么? I have attached the code with this question

1 个答案:

答案 0 :(得分:1)

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

我没有获得带有这些css元素的a个标签。这似乎基于标头中设置的User-Agent属性。

所以有几个选择:

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

  2. 这就是我的卷曲的视频链接标记

    
    
    <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>
    &#13;
    &#13;
    &#13;

    正如您所见,这些课程不存在。

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

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

    (你将不得不搞乱正则表达式,它不是完美的

    1. 使用Youtube API

    2. 我想说这将是优先考虑的方法,只是猜测你想要做什么。特别是search api他们甚至有python片段