PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 8, 2022

[FIXED] How to extract class name as string from first element only?

 July 08, 2022     beautifulsoup, class, python, request, web-scraping     No comments   

Issue

New to python and I have been using this piece of code in order to get the class name as a text for my csv but can't make it to only extract the first one. Do you have any idea how to ?

    for x in book_url_soup.findAll('p', class_="star-rating"):
        for k, v in x.attrs.items():
            review = v[1]
            reviews.append(review)
            del reviews[1]
            print(review)

the url is : http://books.toscrape.com/catalogue/its-only-the-himalayas_981/index.html

the output is:

Two
Two
One
One
Three
Five
Five

I only need the first output and don't know how to prevent the code from getting the "star ratings" from below the page that shares the same class name.


Solution

Instead of find_all() that will create a ResultSet you could use find() or select_one() to select only the first occurrence of your element and pick the last index from the list of class names:

soup.find('p', class_='star-rating').get('class')[-1]

or with css selector

soup.select_one('p.star-rating').get('class')[-1]

In newer code also avoid old syntax findAll() instead use find_all() - For more take a minute to check docs

Example
from bs4 import BeautifulSoup
import requests

url = 'http://books.toscrape.com/catalogue/its-only-the-himalayas_981/index.html'
page = requests.get(url).text
soup = BeautifulSoup(page)

soup.find('p', class_='star-rating').get('class')[-1]
Output
Two


Answered By - HedgeHog
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing