6

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.

Öskå
  • 163
  • 6
  • Can u share the channel id cause I tried with this [code](https://codeshare.io/2Kzdvo) and I am able to get all 30 the ids when calling this channel video URL for example this URL [https://www.youtube.com/user/videos](https://www.youtube.com/user/videos) no consent stuff I got access to json data follow this [code](https://codeshare.io/2Kzdvo) and see if it goes through or share the channel id where you getting the error. – J B Apr 03 '21 at 20:21
  • I am not able to reproduce that consent message stuff in my browser or else would have done something by looking at network requests. Edited Can you share the country of ip address you accessing so i could try via proxy of that country and see I get same consent message or no. – J B Apr 03 '21 at 20:37
  • damn I used France IP from ipvanish and even used incognito so no cookies would be sent then also it directly redirected me to video page no sign of consent message like yours. – J B Apr 03 '21 at 20:50
  • Try to disable cookies at all. – Corvax Apr 03 '21 at 20:52
  • @Öskå Problem in your post example that you don't delete first response cookies, but update them. I'm not sure how to do that, but `requests.get('https://www.youtube.com/user/YouTube/videos', cookies={'CONSENT': 'PENDING+999'})` works for me. – Corvax Apr 03 '21 at 21:29

2 Answers2

6

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))})
Corvax
  • 544
  • 5
  • 9
0

Google is a hazzle and tries to identify you with those technices. There seems now way around as to keep the consent cookie - or you have to give consent every time