David
David

Reputation: 487

Web scraping - Get text from a class with BeautifulSoup and Python?

I want to scrape the text ("Showing 650 results") from a website.

The result of I am looking for is:

 Result : Showing 650 results

The following is the Html code:

<div class="jobs-search-results__count-sort pt3">
            <div class="jobs-search-results__count-string results-count-string Sans-15px-black-55% pb0 pl5 pr4">
                Showing 650 results
            </div>

Python code:

    response = requests.get(index_url)
    soup = BeautifulSoup(response.text, 'html.parser')
    text = {}
    link = "jobs-search-results__count-string results-count-string Sans-15px-black-55% pb0 pl5 pr4" 
    for div in soup.find_all('div',attrs={"class" : link}):
        text[div.text]
    text

So far it looks like my code is not working.

Upvotes: 4

Views: 14726

Answers (2)

Kevin O.
Kevin O.

Reputation: 444

Old: from BeautifulSoup import BeautifulSoup

"Development on the 3.x series of Beautiful Soup ended in 2011, and the series will be discontinued on January 1, 2021, one year after the Python 2 sunsetting date."

New: from bs4 import BeautifulSoup

"Beautiful Soup is a library that makes it easy to scrape information from web pages. It sits atop an HTML or XML parser, providing Pythonic idioms for iterating, searching, and modifying the parse tree."

Upvotes: 0

cs95
cs95

Reputation: 402483

  1. You don't need soup.find_all if you're looking for one element only, soup.find works just as well

  2. You can use tag.string/tag.contents/tag.text to access inner text


div = soup.find('div', {"class" : link})
text = div.string

Upvotes: 7

Related Questions