我正在开发一个项目来抓取多个推特网址,并将他们的关注者计数分配给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)
输出如下:
LazadaPH 52841
ZALORAPH 29786
ShopeePH 7004
eBayPhilippines 874
beauty_MNL 2469
因为我对python很新(并且不希望问一个多余的问题):但有人可以指导我如何将这个打印输出分配给csv的源和教程,并且必须将其提取为两个列(第1列是网站字符串,第2列是关注者计数)。
有什么建议吗?
非常感谢!
答案 0 :(得分:1)
您可以使用 CSV module
例如:
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
答案 1 :(得分:0)
像这样制作你的打印声明:
print(user,';',num_followers)
因此,它打印&#39 ;;'作为值的分隔符。然后将输出传递给文件:
python yourscript.py > yourcsv.csv