0

thank you in advance for helping me out.

What I want to learn is how to output each sublist into a cell in csv file here are my codes

from bs4 import BeautifulSoup
import urllib.request
import re
import csv
import pandas as pd

rating = []

#first page
url = 'https://www.yelp.com/biz/madame-vo-new-york'
ourUrl = urllib.request.urlopen(url)

soup = BeautifulSoup(ourUrl,'html.parser')

for i in soup.find_all('li', {'lemon--li__373c0__1r9wz margin-b3__373c0__q1DuY padding-b3__373c0__342DA border--bottom__373c0__3qNtD border-color--default__373c0__3-ifU'}):

    rating.append(re.findall('.*[="](.* star rating).*', str(i)))

rating = '\n'.join(str(line) for line in rating)

df = pd.DataFrame({'reviewRating': [rating]})

df.to_csv('c1.csv', index = False)

Here are the output results in 'c1.csv': image1

What I want is to look like this:image2

============same-day update===============

As per SOURAV KUMAR's answer, after I changed my code to

df = pd.DataFrame({'reviewRating': rating})

Here is the result:image3 Which is still not what it shows in "image2"

2
  • I don't understand, what is the difference between output 1 and output 2? Commented Oct 23, 2020 at 16:32
  • Remove this line of code: rating = '\n'.join(str(line) for line in rating) and then make the change as per SOURAV's answer. Commented Oct 23, 2020 at 17:08

1 Answer 1

1

So, there is just one change that can be done here :

Instead of this :

df = pd.DataFrame({'reviewRating': [rating]})

write :

df = pd.DataFrame({'reviewRating': rating})

Then, pandas will directly create series of all elements in different rows instead of taking all of it as in one row.

Output :

>>> df
       reviewRating
0   [5 star rating]
1   [4 star rating]
2   [4 star rating]
3   [2 star rating]
4   [5 star rating]
5   [4 star rating]
6   [5 star rating]
7   [3 star rating]
8   [5 star rating]
9   [5 star rating]
10  [4 star rating]
11  [4 star rating]
12  [5 star rating]
13  [5 star rating]
14  [5 star rating]
15  [4 star rating]
16  [5 star rating]
17  [5 star rating]
18  [5 star rating]
19  [4 star rating]
Sign up to request clarification or add additional context in comments.

1 Comment

Hi SOURAV, thanks for your answer, I still have problem after I changed my codes based on your suggestion, I updated my reason on the post, would you please take a look at it? Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.