简体   繁体   English

Python-将API响应保存到for循环中的csv文件

[英]Python - save API responses to csv file in a for loop

I am trying to scrape lyrics from an api and print responses directly to a csv file, like so: 我正在尝试从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)

however, I only see responses if i comment #outwriter.writerow(lyrics) , otherwise the program halts and does not print lyrics. 但是,如果我评论#outwriter.writerow(lyrics) ,我只会看到响应,否则该程序将停止并且不会显示歌词。

how can I save to csv file every lyrics to its own row, at each iteration? 如何在每次迭代时将每个歌词保存到csv文件到其自己的行?

You probably should indent that for loop to keep the writer open. 您可能应该缩进for循环,以保持编写器处于打开状态。

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

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

You also should decide if you really need to store all_lyrics in memory while you write the same information to the file. 您还应该确定在将相同信息写入文件时是否真的需要将all_lyrics存储在内存中。

You can always re-open the file and get all_lyrics at a later point. 您始终可以重新打开文件,并在以后获取all_lyrics

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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