T
T
tigervvin2022-01-23 17:50:21
Python
tigervvin, 2022-01-23 17:50:21

How to parse here?

BS4 does not help, swears at the encoding of the parsed data, I decided to parse the site completely with selenium
There is html code

<body>
    <div>
        <tr>Содержимое тэга 1</tr>
        <tr>Содержимое тэга 2</tr>
        <tr>Содержимое тэга 3</tr>
        <tr>Содержимое тэга 4</tr>
    </div>
</body>

I want to display all the tr tags in turn, so that I can append to them:
f"<div class='cl'>{переменная в которой лежит Содержимое тэга 1}</div>"

To get this response:
<div class="cl">Содержимое тэга 1</div>
<div class="cl">Содержимое тэга 2</div>
<div class="cl">Содержимое тэга 3</div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AVKor, 2022-01-23
@tigervvin

BS4 doesn't help

Helps.
#!/usr/bin/env python

from bs4 import BeautifulSoup

html = """
<body>
    <div>
        <tr>Содержимое тэга 1</tr>
        <tr>Содержимое тэга 2</tr>
        <tr>Содержимое тэга 3</tr>
        <tr>Содержимое тэга 4</tr>
    </div>
</body>
"""
soup = BeautifulSoup(html, 'html.parser')
res = soup.select('tr')
for item in res:
    print(f"<div class='cl'>{item.text.strip()}</div>")

$ ./test.py
<div class='cl'>Содержимое тэга 1</div>
<div class='cl'>Содержимое тэга 2</div>
<div class='cl'>Содержимое тэга 3</div>
<div class='cl'>Содержимое тэга 4</div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question