Parsing HTML with BeautifulSoup

后端 未结 2 649
遇见更好的自我
遇见更好的自我 2020-12-20 01:36

\"enter

(Picture is small, here is another link: http://i.imgur.com/OJC0A.png)

2条回答
  •  余生分开走
    2020-12-20 02:08

    http://www.crummy.com/software/BeautifulSoup/bs4/doc/#strings-and-stripped-strings suggests that the .strings method is what you want - it returns a iterator of each string within the object. So if you turn that iterator into a list and take the last item, you should get what you want. For example:

    $ python
    >>> import bs4
    >>> text = '
    unwanted
    wanted
    ' >>> soup = bs4.BeautifulSoup(text) >>> soup.find_all("div", style="mine")[0].text u'unwantedwanted' >>> list(soup.find_all("div", style="mine")[0].strings)[-1] u'wanted'

提交回复
热议问题