BeautifulSoup: Can't convert NavigableString to string

后端 未结 4 1216
没有蜡笔的小新
没有蜡笔的小新 2021-01-18 06:51

I\'m starting to learn Python and I\'ve decided to code a simple scraper. One problem I\'m encountering is I cannot convert a NavigableString to a regular string.

U

相关标签:
4条回答
  • 2021-01-18 07:31

    For Python 3, the answer is merely str(tag.string)

    Other answers will fail.

    unicode() is not a built-in in Python 3.

    tag.string.encode('utf-8') will convert the string to a byte string, which you don't want..

    0 讨论(0)
  • 2021-01-18 07:43

    I came up to this question and got it solved best by the answer of Mark Ramson from How to remove this \xa0 from a string in python? with

    import unidecode
    word = unidecode.unidecode(tag.string)
    
    0 讨论(0)
  • 2021-01-18 07:44

    You can do this:

    unicode(tag.string)
    
    0 讨论(0)
  • 2021-01-18 07:49

    I tried to decode when I should have encoded:

    str(child.encode('utf-8'))
    
    0 讨论(0)
提交回复
热议问题