beautiful soup just get the value inside the tag

后端 未结 5 1362
名媛妹妹
名媛妹妹 2020-12-06 05:29

The following command:

volume = soup.findAll(\"span\", {\"id\": \"volume\"})[0]

gives:



        
相关标签:
5条回答
  • 2020-12-06 05:35

    There is a function for getting the value of the tag : tag.contents[0]

    Try this :

    volumes = soup('span')
    for volume in volumes:
         print(volume.contents[0])
    
    0 讨论(0)
  • 2020-12-06 05:40

    Using css selector:

    >>> soup.select('span#volume')[0].text
    u'16,103.3'
    
    0 讨论(0)
  • 2020-12-06 05:42

    Extract the string from the element:

    volume = soup.findAll("span", {"id": "volume"})[0].string
    
    0 讨论(0)
  • 2020-12-06 05:44

    Just to add , I also found the .string dosn't do well when there is <br> in the text.

    EG:

     <div class = "Lines">
        <span> First Line <br> Second Line <br> Third Line </span>
      </div>
    

    If we do a soup.find("div",attrs={"class":"Lines}).span.string we get a None

    But a soup.find("div",attrs={"class":"Lines}).span.text we get

    First Line
    Second Line
    Third Line
    

    I think the .string gives a NavigatableString object and .text gives a unicode object.

    0 讨论(0)
  • 2020-12-06 05:56

    try this:

    for a in volume:
        a.get_text()
    

    recent response to this question

    managing exceptions 1

    managing exceptions 2

    0 讨论(0)
提交回复
热议问题