Extract text between link tags in python using BeautifulSoup

后端 未结 3 743
一向
一向 2021-01-16 00:30

I have an html code like this:

My HomePage

<

3条回答
  •  迷失自我
    2021-01-16 00:40

    The following code extracts text (link descriptions) between 'a' tags and stores in an array.

    >>> from bs4 import BeautifulSoup
    >>> data = """

    My HomePage

    ... ...

    Sections

    """ >>> soup = BeautifulSoup(data, "html.parser") >>> reqTxt = soup.find_all("h2", {"class":"title"}) >>> a = [] >>> for i in reqTxt: ... a.append(i.get_text()) ... >>> a ['My HomePage', 'Sections'] >>> a[0] 'My HomePage' >>> a[1] 'Sections'

提交回复
热议问题