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
文件每个歌词到自己的行,在每次迭代?
'[h.extract()for h(html)'('script')]'自己没做什么......你想保存那个列表吗? –