J
J
Jungles2020-08-24 12:15:07
Python
Jungles, 2020-08-24 12:15:07

How to make a non unicode string?

beautifulsoup documentation - https://www.crummy.com/software/BeautifulSoup/bs4/...

we can decode a NavigableString into a regular unicode string, which NavigableString is not.
Visually, they do not differ

type(soup.h1.text)
>>>class 'str'

type(soup.h1.string)
>>>class 'bs4.element.NavigableString'


How to make it return not a string of type unicode-string, but a string of a valid NavigableString or its own class?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Deleting Account, 2020-08-24
@Jungles

Just create a new class that accepts a string like

class CustomStr:
    def __init__(self, str_):
        self.__str = str_

    def d_space(self):
        return "".join(self.__str.split())

S
Sergey Karbivnichy, 2020-08-24
@hottabxp

Didn't wash anything. Here:

soup = BeautifulSoup(html,"html.parser")
  var = soup.title.string
  print(var)
  print(type(var))
In var there will be just NavigableString:
Hello World! Site Title
<class 'bs4.element.NavigableString'>

Here is Russian documentation: NavigableString

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question