how to write multiple lines in a file using python -
i know how write multiple lines file, if know how many want write. but, problem comes when want write multiple lines, but, don't know how be
i working on app scraps website , stores links of result in text file. but, don't know how many lines reply with. code follows right now.
r = requests.get('http://www.crunchyroll.com/i-cant-understand-what-my-husband-is-saying') soup = beautifulsoup(r.text) print soup.title subtitles = soup.findall('div',{'class':'wrapper container-shadow hover-classes'}) episode in subtitles: x = episode.find_all('a') in x: #print a['href'] z = a['href'] l = 'http://www.crunchyroll.com'+ z print l this gives me desired output.so,i tried write stuff in file adding :
file = open("batchlinks.txt", "w") file.write(l) file.close() but, unfortunately, writes first link. how can add other links too?
make sure write link inside loop. using with command save manually closing file well. should work:
r = requests.get('http://www.crunchyroll.com/i-cant-understand-what-my-husband-is-saying') soup = beautifulsoup(r.text) print soup.title subtitles = soup.findall('div',{'class':'wrapper container-shadow hover-classes'}) open("batchlinks.txt","w") file: episode in subtitles: x = episode.find_all('a') in x: z = a['href'] link = 'http://www.crunchyroll.com'+ z print link file.write(link)
Comments
Post a Comment