'Please how do i split this list using the split method in python

Edited

I am trying to scrape avg_ratings, ratings and published year on goodreads so i can put them in a separate list

import requests from bs4 import BeautifulSoup as bs

url = "https://www.goodreads.com/shelf/show/business"

response = requests.get(url)
soup = bs(response.content, "lxml")


avg_ratings = soup.find_all("span", class_="greyText")
for ratings in avg_ratings:
    print(ratings.get_text().split("-"))

Result:

['\n                avg rating 4.18 —\n                272,533 ratings  —\n                published 2014\n              ']
['(Goodreads Author)']
['\n                avg rating 4.11 —\n                277,455 ratings  —\n                published 2011\n              ']
['(Goodreads Author)']
['\n                avg rating 4.11 —\n                277,455 ratings  —\n                published 2011\n              ']
['\n                avg rating 4.21 —\n                790,527 ratings  —\n                published 1936\n              '

]



Solution 1:[1]

your selector need to be more specific or you get unwanted element, use .tableList .greyText.smallText

import requests
from bs4 import BeautifulSoup
import re

page = requests.get('https://www.goodreads.com/book/most_read').text
soup = BeautifulSoup(page, 'html.parser')

avg_ratings = soup.select(".tableList .greyText.smallText")
for rating in avg_ratings:
    ratings = re.sub(r"\s+", ' ', rating.text) # clean "published\n               2017"
    ratings = [x.strip() for x in ratings.split("—")]
    print(ratings)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 uingtea