I am trying to solve my problems making searches, reading documentations.
The problem
I want to get all youtube titles from an youtube channel using python beautiful soup. Youtube loads dynamically, i think with JavaScript, without pyqt5 I just can not get any title, So i used the pyqt5 I was able to get titles from youtube channel. The problem is that i need to load all the videos. I can just load the 29 ou 30 first ones. I am thinking on simulating a scroll down or somthing like that. I can not find how to do this on beatiful soup without selenium...
Version and libraries used:
- Python 3.7.4
- BeautifulSoup4
- riverbankcomputing.com
I am trying not using selenium or scrapy
- I want to understand what i can and what i can not do with BeautifulSoup on dynamic pages.
The code
from bs4 import BeautifulSoup
import sys
import urllib.request
from PyQt5.QtWebEngineWidgets import QWebEnginePage
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
class Page(QWebEnginePage):
def __init__(self, url):
self.app = QApplication(sys.argv)
QWebEnginePage.__init__(self)
self.html = ''
self.loadFinished.connect(self._on_load_finished)
self.load(QUrl(url))
self.app.exec_()
def _on_load_finished(self):
self.html = self.toHtml(self.Callable)
print('Load finished')
def Callable(self, html_str):
self.html = html_str
self.app.quit()
def main():
page = Page(
'https://www.youtube.com/channel/UCY50YjnaeI7ezvRagFWeWmw/videos')
soup = BeautifulSoup(page.html, 'html.parser')
what_i_search = soup.find('div', class_='style-scope ytd-grid-renderer')
youtube_titles = what_i_search.find_all('h3')
line = 0
for youtube_title in youtube_titles:
line = line + 1
print(str(line) + ". " + youtube_title.text)
if __name__ == '__main__':
main()
Output:
Load finished
1. Plants vs Zombies 2 - FUTURO DISTANTE - dias 09, 10, 11 e 12 - Parte 48
2. Marisa ou M�nica - Turma da M�nica
3. Jogo PET DOLL - Parte 02
4. A Magali n�o mora mais aqui - Turma da M�nica
5. MOY'S WORLD (parte 03) - Mundo 01 - N�vel 6 - Gameplay
6. M�nica e Cebolinha em "uma pedra no meio do caminho" - Turma da M�nica
7. Plants vs Zombies 2 - FUTURO DISTANTE - dia 08 e Terror do Amanh� - Parte 47
8. Magali em "N�o Sou Boneca" - Turma da M�nica
9. Talking Tom HERO DASH - Terminamos todas os mundos!!! - Parte 13
10. M�nica em Maternidade Saud�vel - Turma da M�nica
11. Rotina Matinal da Beb� Reborn da Luisa - Cuidando da boneca
12. Bloop Go! Jogo/Game - Parte 1
13. Chico Bento em "se n�o fosse a fei�ra" - Turma da M�nica
14. Luisa cuidando do gatinho BUBBU - parte 3
15. Turma da M�nica em "as tr�s letrinhas" - Feliz dia das m�es!!!
16. Plants vs Zombies 2 - FUTURO DISTANTE - dias 05, 06 e 07 - Parte 46
17. A Turma em "fregu�s tem sempre raz�o" - Turma da M�nica
18. Dollify - parte 4 - Brincando de fazer caricaturas! Fazendo o boneco da prima Julia
19. Casc�o em "o maior bode" - Turma da M�nica
20. Jogo Hotel Transilv�nia Adventures - parte 12 - Terminamos a COZINHA - Fases 55 e 56
21. Turma do Penadinho em "sexta-feira 14" - Turma da M�nica
22. Plants vs Zombies 2 - iniciamos FUTURO DISTANTE - dias 01, 02, 03 e 04 - Parte 45
23. Chico Bento e o Nome - Turma da M�nica
24. Talking Angela - Jogando com a Gatinha Angela - N�vel 20 (parte 9)
25. Magali e a Turma em "OS ADOLESCENTES" - Turma da M�nica
26. Cortando o cabelo (franjinha) da Luisa em casa! #m�eefilha
27. Jogo Gardenscapes - Parte 11 - Cuidando do Jardim e do Tot� - N�vel 48
28. Cebolinha em "a caveira dentu�a" - Turma da M�nica
29. Plants vs Zombies 2 - TERMINAMOS A CIDADE PERDIDA - dias 30, 31 e 32 - Parte 44
30. Magali em Comida Falante - Turma da M�nica