【问题标题】:Why is youtube not giving me video links and only giving me their resource pages?为什么 youtube 不给我视频链接,只给我他们的资源页面?
【发布时间】:2021-09-15 03:23:34
【问题描述】:

由于某种原因,youtube 只给我他们的资源页面,而不是给我他们的视频链接 这就是我要找的。​​p>

代码:

import mechanize
import urllib
import urllib.parse as urlparse

url = "https://www.youtube.com"
browser = mechanize.Browser()

browser.open(url)

for link in browser.links():
    new_url = urlparse.urljoin(link.base_url,link.url)
    b1 = urlparse.urlparse(new_url).hostname
    b2 = urlparse.urlparse(new_url).path
    print('http://'+b1+b2)

我得到的输出:

http://accounts.google.com/ServiceLogin
<http://www.youtube.com/
http://www.youtube.com/

我期望的输出:

https://www.youtube.com/watch?v=uVvZlH5gPA
https://www.youtube.com/watch?v=uVvasdad5

【问题讨论】:

    标签: python web-scraping youtube mechanize


    【解决方案1】:

    tldr:YouTube 不希望你抓取它

    在开始抓取链接之前,我会首先检查您首先返回的网站。 YouTube 做了各种各样的事情来防止抓取变得容易。如果您不熟悉抓取和库,我建议您先从简单的示例开始学习。

    我运行了您的代码,将响应保存为html,并在浏览器中查看了它。这显然不是您期望的标准网站。

    # example.py
    import mechanize
    
    print(mechanize.Browser.open(https://www.youtube.com).read())
    
    python3 example.py > example.html
    

    这是我用浏览器打开example.html时得到的:

    如果你需要爬取 YouTube,互联网上肯定有很多很好的教程。您可能需要使用不同的库,例如 Selenium,或者以一种对 YouTube 隐藏您不是普通用户而是 Python 脚本的方式设置您的 cookie。

    【讨论】:

      猜你喜欢
      • 2018-04-12
      • 1970-01-01
      • 1970-01-01
      • 2021-05-01
      • 2019-08-08
      • 1970-01-01
      • 2014-10-31
      • 2021-09-07
      • 2013-11-18
      相关资源
      最近更新 更多