【问题标题】:Python Beautifulsoup can't find tags that are in web browserPython Beautifulsoup 找不到 Web 浏览器中的标签
【发布时间】:2020-09-14 17:00:33
【问题描述】:

我目前正在尝试抓取这个网站: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 个

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 当我访问view-source:http://www.laprensa.com.ar/并搜索</article>时,实际上只有10个......
  • 当我在浏览器中看到 html 有超过 10 个

标签: python beautifulsoup python-requests tags findall


【解决方案1】:

文章通过 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- [...]

【讨论】:

  • 所以只是为了 tldr,这是因为您在 requests.get() 上使用了 content 属性而不是 text
  • @notacorn 否,.content 返回原始字节,.text 解码字符串。这与 OP 的要求不同。
  • @notacorn 我正在模拟页面加载所有文章的请求。当使用加载页面时 - 它是空的,JavaScript 然后向http://www.laprensa.com.ar/json/apps/notesHome.aspx?category=... 发出请求并动态加载内容。
猜你喜欢
  • 2020-06-04
  • 1970-01-01
  • 1970-01-01
  • 2017-11-22
  • 1970-01-01
  • 1970-01-01
  • 2017-04-18
  • 2022-10-04
相关资源
最近更新 更多