-2

I am working on a project to scrape multiple twitter URL's and assign their follower count to a csv:

username= ['LazadaPH','ZALORAPH','ShopeePH','eBayPhilippines','beauty_MNL']

for user in username:
   url = 'https://www.twitter.com/'+ user
   r = requests.get(url)
   soup = BeautifulSoup(r.content,'lxml')
   f = soup.find('li', class_="ProfileNav-item--followers")
   title = f.find('a')['title']
   num_followers = int(title.split(' ')[0].replace(',',''))
   print(user,num_followers)

The output looks as follows:

LazadaPH 52841
ZALORAPH 29786
ShopeePH 7004
eBayPhilippines 874
beauty_MNL 2469

Since I'm quite new to python (and don't hope to be asking a redundant question): but can someone guide me to sources and tutorials of how to assign this printed output to a csv and essentialy extract it into two columns (column 1 is website string and column 2 the follower count).

Any suggestions?

Thanks a bunch!

calicationoflife
  • 281
  • 2
  • 8
  • 17
  • Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, [describe the problem](https://meta.stackoverflow.com/questions/254393/what-exactly-is-a-recommendation-question) and what has been done so far to solve it. – Keyur Potdar Feb 28 '18 at 07:47
  • There are quite many posts about output result to csv, e.g. [this](https://stackoverflow.com/questions/37289951/python-write-to-csv-line-by-line) – pe-perry Feb 28 '18 at 07:48

2 Answers2

1

You can use the CSV module

Ex:

import csv
with open('out.csv', 'w') as csvfile:
    r = csv.writer(csvfile, delimiter=',')     #   ----> COMMA Seperated
    for user in username:
       url = 'https://www.twitter.com/'+ user
       r = requests.get(url)
       soup = BeautifulSoup(r.content,'lxml')
       f = soup.find('li', class_="ProfileNav-item--followers")
       title = f.find('a')['title']
       num_followers = int(title.split(' ')[0].replace(',',''))
       r.writerow([user,num_followers])    #  ----> Adding Rows
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

Make your print statement like this: print(user,';',num_followers) So that it prints ';' as separator for values. Then pipe the output to a file:

python yourscript.py > yourcsv.csv 
Ruhshan
  • 121
  • 1
  • 8
  • csv needs a comma (`,`) not a semi-colon. – Keyur Potdar Feb 28 '18 at 08:24
  • @KeyurPotdar we call it "csv" but there are quite few dialects using different separators - comma, semi-colon, tab, pipe etc... – bruno desthuilliers Feb 28 '18 at 08:28
  • Actually, I tried your example, and it's giving all the output in one cell if you open the csv in excell. I don't have much knowledge regarding this. So, sorry if I missed something. – Keyur Potdar Feb 28 '18 at 08:32
  • You need to change the delimiter in excel to ';' . I apologize that I don't have excel right now thus can't point out the steps to you. – Ruhshan Feb 28 '18 at 10:53