Python - 在for循环中保存对csv文件的API响应

时间:2017-09-23 23:26:13

标签: python csv

我正在尝试从api中删除歌词并将响应直接打印到csv文件,如下所示:

def scrape_genius_lyrics(urls):

    all_lyrics=[]

    headers = {'Authorization': 'mytoken'}
    base_url = 'https://genius.com/'

    with codecs.open('genius.csv', 'ab', encoding='utf8') as outputfile:
        outwriter = csv.writer(outputfile)

    for url in urls:
        page_url = base_url + url
        try:
            page = requests.get(page_url, headers=headers)
            html = BeautifulSoup(page.text, "html.parser")
            [h.extract() for h in html('script')]
            lyrics = html.find('div', class_='lyrics').get_text()         
            # outwriter.writerow(lyrics)
            all_lyrics.append(lyrics)
            print lyrics
        except:
            'could not find page for {}'.format(url)

然而,如果我评论#outwriter.writerow(lyrics),我只会看到回复,否则程序会停止并且不会打印歌词。

如何在每次迭代时将每个歌词保存到csv文件到自己的行?

1 个答案:

答案 0 :(得分:0)

您可能应该缩进for循环以保持编写器打开。

with codecs.open('genius.csv', 'ab', encoding='utf8') as outputfile:
    outwriter = csv.writer(outputfile)

    for url in urls:
        page_url = base_url + url
        ...

在将相同的信息写入文件时,您还应该确定是否确实需要将all_lyrics存储在内存中。

您可以随时重新打开该文件并稍后获取all_lyrics