2017-09-23 69 views
0

我想直接刮去的API歌词和打印的响应到csv文件,像这样:Pyhton - API响应保存到CSV文件中的一个for循环

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文件每个歌词到自己的行,在每次迭代?

+0

'[h.extract()for h(html)'('script')]'自己没做什么......你想保存那个列表吗? –

回答

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