CSV写入每个字母而不是每个列表索引

时间:2017-11-12 15:23:16

标签: python csv

import requests
import csv
from bs4 import BeautifulSoup

def GetLinks(url):
    list=[]
    html=requests.get(url).text
    soup=BeautifulSoup(html,'lxml')
    for l in soup.select('h3.lvtitle > a'):
        list.append(l.get('href'))
    return list

url_list=GetLinks('https://www.ebay.com/sch/i.html?_from=R40&_sacat=0&_nkw=fitness+tracker+115&_ipg=200&rt=nc')

with open('EbayTestFil.csv','w') as f:
    writer = csv.writer(f)
    writer.writerows(url_list)

我的列表'url_list'中的每个索引在我的CSV文件中看起来都是这样的:

 h,t,t,p,s,:,/,/,w,w,w,.,e,b,a,y,.,c,o,m,/,i,t,m,/,B,l,u,e,t,o,o,t,h,-,S,m,a,r,t,-,W,a,t,c,h,-,I,D,1,1,5,-,W,r,i,s,t,b,a,n,d,-,B,r,a,c,e,l,e,t,-,P,e,d,o,m,e,t,e,r,-,S,p,o,r,t,-,F,i,t,n,e,s,s,-,T,r,a,c,k,e,r,/,2,9,2,3,0,1,5,4,4,9,4,4,?,h,a,s,h,=,i,t,e,m,4,4,0,e,8,7,9,d,f,0,:,m,:,m,d,W,4,X,F,S,B,r,N,B,L,B,F,s,V,d,e,h,T,p,N,A

当然需要像这样:

 https://www.ebay.com/itm/Bluetooth-Smart-Watch-ID115-Wristband-Bracelet-Pedometer-Sport-Fitness-Tracker/292301544944?hash=item440e879df0:m:mdW4XFSBrNBLBFsVdehTpNA

你能帮我找到我的错误吗?

1 个答案:

答案 0 :(得分:1)

您将返回整个列表,因此请尝试编写列表中的每个项目

with open('EbayTestFil.csv','w') as f:
    writer = csv.writer(f)
    for item in url_list:
        writer.writerow([item])