Get value of span tag using BeautifulSoup

前端 未结 4 2132
滥情空心
滥情空心 2021-02-20 11:47

I have a number of facebook groups that I would like to get the count of the members of. An example would be this group: https://www.facebook.com/groups/347805588637627/ I have

相关标签:
4条回答
  • 2021-02-20 12:10

    If you have more than one span tag you can try this

    from bs4 import BeautifulSoup
    
    soup = BeautifulSoup(html, 'html.parser')
    
    tags = soup('span')
    
    for tag in tags:
      print(tag.contents[0])
    
    0 讨论(0)
  • 2021-02-20 12:13

    In case there is more than one span tag in the page:

    from bs4 import BeautifulSoup
    soup = BeautifulSoup(your_html_input, 'html.parser')
    span = soup.find("span", id="count_text")
    span.text
    
    0 讨论(0)
  • 2021-02-20 12:15

    Facebook uses javascrypt to prevent bots from scraping. You need to use selenium to extract data on python.

    0 讨论(0)
  • 2021-02-20 12:19

    You can use the text attribute of the parsed span:

    >>> from bs4 import BeautifulSoup
    >>> soup = BeautifulSoup('<span id="count_text">9,413 members</span>',   'html.parser')
    >>> soup.span
    <span id="count_text">9,413 members</span> 
    >>> soup.span.text
    '9,413 members'
    
    0 讨论(0)
提交回复
热议问题