接受YouTube的Cookie同意 [英] Accept cookies consent from Youtube

查看:81
本文介绍了接受YouTube的Cookie同意的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从YouTube频道中检索YouTube视频列表,例如"https://www.youtube.com/user/YouTube/videos",以获取第n个视频(感谢 key ="videoId" ).直到几天前,当它开始征得我的同意时,它才像魅力一样起作用.

我在运气上尝试了很多事情,但是仍然看到消息,要求我接受Cookie才能观看视频.

 导入请求汇入url ='https://www.youtube.com/user/YouTube/videos's1 = request.session()s1.get(网址)打印(原始Cookie")打印(s1.cookies)cookieValueNum =(re.findall(r'\ d +',str(s1.cookies)))[0]cookieValue =('YES + cb.20210328-17-p0.en-GB + FX +'+ str(cookieValueNum))cookie = {'name':'CONSENT','value':cookieValue,'domain':'.youtube.com'}打印("==========")打印(添加新Cookie之后")s1.cookies.update(cookie)打印(s1.cookies)打印(s1.get(URL,Cookies = cookie).text) 

它仍然返回相同的消息,要求我同意cookie(显然,在html中,这是我在私人会话中打开Youtube时得到的图片):

然后我的想法是复制 Consent cookie,然后将其发送回去,以便能够访问页面内容.

任何关于我做错事情的想法吗?这个想法不是使用Youtube API,而是仅在需要时使用request/BeautifulSoup.

解决方案

您需要删除第一个响应cookie.我不确定如何在request.session中做到这一点,但是以下任何一项对我来说都是有效的.

  requests.get('https://www.youtube.com/user/YouTube/videos',Cookies = {'CONSENT':'PENDING + 999'})requests.get('https://www.youtube.com/user/YouTube/videos',Cookies = {'CONSENT':'YES + cb.20210328-17-p0.en-GB + FX + {}'.format(random.randint(100,999))}) 

I'm trying to retrieve a list of Youtube videos from a Youtube channel, say "https://www.youtube.com/user/YouTube/videos", to get the nth first videos (thanks to the key = "videoId"). It used to work like a charm until a few days ago, when it started to ask for my consent.

I tried many things on SO with no luck, I still see the message asking me to accept the cookies in order to see the videos.

import requests
import re

url='https://www.youtube.com/user/YouTube/videos'
s1 = requests.session()
s1.get(url)
print("Original Cookies")
print(s1.cookies)
cookieValueNum = (re.findall(r'\d+', str(s1.cookies)))[0]
cookieValue = ('YES+cb.20210328-17-p0.en-GB+FX+'+str(cookieValueNum))
cookie = {'name': 'CONSENT', 'value': cookieValue, 'domain': '.youtube.com'}
print("==========")
print("After new Cookie added")
s1.cookies.update(cookie)
print(s1.cookies)
print(s1.get(url, cookies=cookie).text)

It still returns the same message asking my consent for cookies (in html obviously, this is a picture of what I get when opening Youtube in a private session):

My idea was then to replicate the Consent cookie and sent it back to be able to access the page content.

Any idea of what I'm doing wrong? The idea is not to use the Youtube API but only request/BeautifulSoup if needed.

解决方案

You need to delete first response cookies. I'm not sure how to do that in requests.session, but any of the following works for me.

requests.get('https://www.youtube.com/user/YouTube/videos', cookies={'CONSENT': 'PENDING+999'})

requests.get('https://www.youtube.com/user/YouTube/videos', cookies={'CONSENT': 'YES+cb.20210328-17-p0.en-GB+FX+{}'.format(random.randint(100, 999))})

这篇关于接受YouTube的Cookie同意的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆