单击带有 Python 的按钮以加载 HTML

Clicking a button with Python to load HTML

我正在制作一个 python 从 Youtube 播放列表中抓取信息的脚本。

我的问题是我的脚本不适用于包含 100 多个视频的播放列表,因为 Youtube 要求您单击页面底部的 'Load more' 按钮(参见下面的代码和图片)才能加载更多播放列表。

我的问题是如何使用 Python 单击此按钮以加载页面的其余部分?

<button class="yt-uix-button yt-uix-button-size-default yt-uix-button-default load-more-button yt-uix-load-more browse-items-load-more-button" type="button" onclick=";return false;" aria-label="Load more
" data-uix-load-more-target-id="pl-load-more-destination" data-uix-load-more-href="/browse_ajax?    action_continuation=1&amp;continuation=4qmFsgIuEiRWTFBMQjZKLXhzOVhod0IyRHY2ZV9rR0t4Z2xEOVNVdDBVSmQaBkNHVSUzRA%253D%253D"><span class="yt-uix-button-content">  <span     class="load-more-loading hid">
  <span class="yt-spinner">
  <span title="Loading icon" class="yt-spinner-img  yt-sprite"></span>

Loading...
  </span>

  </span>
  <span class="load-more-text">
    Load more

  </span>
</span></button>

通常单击按钮会提交一个表单。此表单提交是一个 HTTP POST 请求。我通过点击 next 按钮检查了 youtube(使用 Chrome 开发工具),发现它是 XHR GET HTTP 请求。见下图:

因此,在您最初获得一个页面后,您可能会通过请求加载更多(第 2,3 页等): https://www.youtube.com/results?search_query=<search term>&page=2&spf=navigate 而不是 page=2 您在行中设置下一页,例如。 page=3 等等

回复将是格式良好的JSON,包含您需要的所有信息。