简体   繁体   中英

Python - assign print output csv

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!

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

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM