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.
Advertisement
Answer
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))})