How to extract a specified element with bs4?

clipboard.png
how to extract these two values

Mar.09,2021

you need to know the basics of HTML, and then consult the bs4 documentation.

  1. HTML5, http://www.html5-tutorials.org/
  2. bs4, https://www.crummy.com/softwa.
< hr >

Please refer to the following code

-sharp -*- coding: utf-8 -*-
'''
@author 
'''
from bs4 import BeautifulSoup


html_doc = '''
<html>
  <head></head>
  <body>
  <div class="content-right">
    <h1 class="app-name">
      <span></span>
    </h1>
    <div class="detail">
      <span class="download-num">:1950</span>
    </div>
  </div>
  </body>
</html>
'''
soup = BeautifulSoup(html_doc, 'html.parser')

cont_right = soup.select('body div.content-right')[0]

app_name = cont_right.select('h1.app-name span')[0]
print('app-name: {}'.format(app_name.string))

down_num = cont_right.select('div.detail span.download-num')[0]
print('download-num:: {}'.format(down_num.string))

use the strings parameter of BeautifulSoup to return the generator of the string in the tag, and navigate to 'that span' and then use strings.

strings
Menu