D
D
Dmitry Bely2020-05-01 00:59:57
Python
Dmitry Bely, 2020-05-01 00:59:57

How to get data from a div block that is written to from JS?

Hello. I'm learning python, interested in element parsing. The essence of the question is how to get data from the div block into which are generated via JS. I received data from the elements, through BeautifulSoup, and everything worked out. But when I tried to get data from the block, I got either None or []. Example: There is a broadcast on YouTube, there is a block under the live broadcast: There are n-th number of viewers watching now. Here's how to get these numbers? I apologize for the inaccuracies, I hope I conveyed the essence of the issue. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Karbivnichy, 2020-05-01
@Bunt

BeautifulSoup on YouTube won't help you (although you can get the page using selenium and then parse it with BeautifulSoup - but that's another story)
On YouTube pages, the data is stored in Json. You can cut them out from there and work with them as with json. You can try to search corny using the Python find function:
(Yes, the Pythonists will forgive me for this code))
I'm not saying that this is correct, and this is how it should be done, but it works, and thanks for that.

import requests

headers = {'user-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:72.0) Gecko/20100101 Firefox/72.0',
      'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
}
url = 'https://www.youtube.com/watch?v=pSWOcXg703s'

response = requests.get(url,headers=headers)
html = response.text
first_1 = html.find('og:title" content="')+19
second_2 = html.find('">',first_1)
title = html[first_1:second_2]

first = html.find('videoViewCountRenderer')+72
second = html.find('"}]}',first)
views = html[first:second]

first_ = html.find('likeStatus":"INDIFFERENT","tooltip"')+37
second_ = html.find('"}},',first_)
likes = html[first_:second_].replace(' ','').split('/')

print(f'Название видео: {title}')
print(f'Сейчас смотрят: {views}')
print(f'Лайков: {likes[0]}')
print(f'Дизлайков: {likes[1]}')

At the exit:
Название видео: Elon Musk Live: Bitcoin BTC Talk, BTC Mass Adoption & SpaceX update [April, 2020]
Сейчас смотрят: 59 365
Лайков: 2 542
Дизлайков: 153

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question