[Question] Python string replacement.replace () will replace even the ones that are not specified.

This is the first post. Thank you.

I'm trying to scrape my own site. Data fetched

<div class="st-kaiwa-hukidashi">Target character string</div>
<div class="st-kaiwa-hukidashi2">Target character string</div>

I want to keep only the "target character string" of.

Therefore,

for num in ["", "2"]:
    kaiwa = "div.st-kaiwa-hukidashi{}" .format(num)
    talk = soup.select(kaiwa)
    for s in talk :
        trash = "<div class={}>".format(kaiwa)
        t = s.text.replace("trash" , '')
        print(t)

When I executed, I was able to extract only the "target character string" cleanly.

In this code,

<div class="st-kaiwa-hukidashi">
<div class="st-kaiwa-hukidashi2">

I was planning to remove only

</div>

It looks like it has been replaced.

I'm sorry to ask you a rudimentary question, but I would like you to teach me why __ Why is it replaced with'' to </ div> __.

Thank you.

Recommended Posts

[Question] Python string replacement.replace () will replace even the ones that are not specified.
Python version (PHP to Python) that deletes the subsequent character string from the specified character string (extension)
Examples and solutions that the Python version specified in pyenv does not run
About the matter that the contents of Python print are not visible in docker logs
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
The story that `while queue` did not work in python
[Python3] "A // B" and "math.floor (A / B)" are not always the same! ??