Python Beautifulsoup找不到网络浏览器中的标签

问题描述 投票:0回答:1

[我目前正在尝试删除此网站:http://www.laprensa.com.ar/在浏览器中查看html,我发现它具有几个称为“文章”的标签,因此我这样做:

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

但是当我能数到30时才找到10

有人可以给我一些帮助吗?

python beautifulsoup python-requests tags findall
1个回答
0
投票

这些文章是通过Javascript注入网站的。您可以使用requests库模拟这些请求。

例如:

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)))

打印:

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- [...]
© www.soinside.com 2019 - 2024. All rights reserved.