Varun Vembar
Varun Vembar

Reputation: 358

Convert a BeautifulSoup ResultSet into a list of strings

I'm trying to scrape the details of the reviews from here into a CSV using Python. Each movie has a star rating, which is denoted by an image, having a class('icon-star-fill' , or 'icon-star-half'). I'm trying to write a function to assign a numerical value.

The code that I have so far is returning a bs4.element.ResultSet, with each element a Tag

    [<i class="icon-star-full"></i>, <i class="icon-star-full"></i>]

I want to convert that into a list of strings, like

    ["<i class="icon-star-full"></i>", "<i class="icon-star-full"></i>"]

I've tried soup_obj.text, soup_obj.content, and they're returning empty strings.

This is my code

    from bs4 import BeautifulSoup
    import requests
    result = requests.get(url='http://www.rogerebert.com/reviews')
    result_content = result.content
    soup_obj = BeautifulSoup(result_content, 'html5lib')
    wrapper_class = soup_obj.find('div', id='review-list')
    for x in wrapper_class.find_all('figure'):
        convoluted_rating = x.find('span', class_='star-rating').find_all('i')
        print convoluted_rating

I've seen this and it returns an array with None, like so

    [None,None]

Upvotes: 2

Views: 6769

Answers (1)

cs95
cs95

Reputation: 402523

You can iterate over the ResultSet and call tag.prettify:

tags = [] 
for x in wrapper_class.find_all('figure'):
    tags.extend(
         (i.prettify() for i in x.find('span', class_='star-rating').find_all('i'))
    )

print(tags)

['<i class="icon-star-full">\n</i>\n',
 '<i class="icon-star-full">\n</i>',
 '<i class="icon-star-full">\n</i>\n',
 ...
]

Upvotes: 3

Related Questions