Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accept cookies consent from Youtube

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):

YT Consent

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.

like image 671
Öskå Avatar asked Apr 03 '21 18:04

Öskå


2 Answers

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))})
like image 147
Corvax Avatar answered Oct 24 '22 12:10

Corvax


I faced the same problem - here's a solution that should work just fine for your case.

with browsers like chrome you can always check what data you need to pass to acccept cookies. you find these information in dev tools -> application -> cookies.

screenshot of the google chrome cookie view

doing this, you'll see that youtube expects YES or NO and any integer > 0.

pass these information in your request. and that's it.

requests.get('https://www.youtube.com/user/YouTube/videos', cookies={'CONSENT': 'YES+1'})
like image 23
chrischma Avatar answered Oct 24 '22 13:10

chrischma