简体   繁体   中英

Python Beautifulsoup can't find tags that are in web browser

I am currently trying to scrape this website: http://www.laprensa.com.ar/ looking at the html in the browser I see that it has several tags called 'article' so I do this:

html = request.get('http://www.laprensa.com.ar/').text
soup = BeautifulSoup(html, 'html5lib')
articles = soup.findAll('article')

but only find 10 when I can count more than 30

can someone give me some help about it?

The articles are injected via Javascript to the site. You can simulate those requests using requests library.

For example:

import requests
from bs4 import BeautifulSoup

url = 'http://www.laprensa.com.ar/'
category_url = 'http://www.laprensa.com.ar/json/apps/notesHome.aspx?category={category_number}'

soup = BeautifulSoup(requests.get(url).content, 'html.parser')

categories = set(a['href'].split('=')[-1] for a in soup.select('a[href^="/category.aspx?category="]'))

all_notes = []
for category in categories:
    u = category_url.format(category_number=category)
    print(u)
    data = requests.get(u).json()
    for note in data['notes']:
        all_notes.append((note['title'], note['link']))

# print all notes:
from textwrap import shorten
for i, (title, link) in enumerate(all_notes, 1):
    print('{:<5} {:<60} {}'.format(i, shorten(title, 60) , shorten(link, 50)))

Prints:

1     Deshielo opositor                                            http://www.laprensa.com.ar/489187-Deshielo- [...]
2     Números alarmantes                                           http://www.laprensa.com.ar/488923-Numeros- [...]
3     ¿Plan económico?                                             http://www.laprensa.com.ar/488627-Plan- [...]
4     Mercosur                                                     [...]
5     Con aliados así                                              http://www.laprensa.com.ar/488120-Con- [...]
6     Cachivache I                                                 [...]
7     Cristina en Olivos                                           http://www.laprensa.com.ar/487641-Cristina- [...]
8     Horacio y las cacerolas                                      http://www.laprensa.com.ar/487426-Horacio-y- [...]
9     Tregua con los medios                                        http://www.laprensa.com.ar/486903-Tregua- [...]
10    Iglesia y marketing                                          http://www.laprensa.com.ar/486394-Iglesia-y- [...]
11    Internas                                                     [...]
12    Doble comando I                                              http://www.laprensa.com.ar/485957-Doble- [...]
13    Lo que vendrá I                                              http://www.laprensa.com.ar/485544-Lo-que- [...]
14    Tropiezo en Roma                                             http://www.laprensa.com.ar/485346-Tropiezo- [...]

...

385   ¿Por qué fracasó el `neoliberalismo'?                        http://www.laprensa.com.ar/489156-Por-que- [...]
386   El liberalismo ante la batalla cultural                      http://www.laprensa.com.ar/488878-El- [...]
387   Historia de la pandemia en dos países: China y Taiwán        http://www.laprensa.com.ar/488800-Historia- [...]
388   Corralito y mercado de cambios                               http://www.laprensa.com.ar/488799-Corralito- [...]
389   El mundo en estado de incertidumbre                          http://www.laprensa.com.ar/488798-El-mundo- [...]
390   Los bancos, siempre atractivos                               http://www.laprensa.com.ar/488879-Los- [...]
391   "El escenario es desafiante para incentivar [...]            http://www.laprensa.com.ar/488604-El- [...]
392   Hacia el empobrecimiento general                             http://www.laprensa.com.ar/488592-Hacia-el- [...]
393   La derecha reformadora (II Parte): de Federico Pinedo [...]  http://www.laprensa.com.ar/488584-La- [...]
394   Razones del fatalismo argentino                              http://www.laprensa.com.ar/488586-Razones- [...]
395   "La intención es noble"                                      http://www.laprensa.com.ar/488595-La- [...]
396   "El Mercosur debe integrarse a otros mercados''              http://www.laprensa.com.ar/488338-El- [...]
397   Un país sin brújula                                          http://www.laprensa.com.ar/488341-Un-pais- [...]
398   La derecha reformadora (I parte) De Carlos Pellegrini [...]  http://www.laprensa.com.ar/488342-La- [...]
399   Urge un cambio conceptual en la política monetaria           http://www.laprensa.com.ar/488343-Urge-un- [...]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM