问题
i know how to write multiple lines to a file, if I know how many I want to write. But, the problem comes when I want to write multiple lines, but, I don't know how much they will be
I am working on an app which scraps from a website and stores the links of the result in a text file. But, we don't know how many lines will it reply with. My code is as 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'})
for episode in subtitles:
x = episode.find_all('a')
for a in x:
#print a['href']
z = a['href']
l = 'http://www.crunchyroll.com'+ z
print l
This gives me the desired output.So,I tried to write stuff up in a file by adding :
file = open("BatchLinks.txt", "w")
file.write(l)
file.close()
But, unfortunately, it only writes the first link. How can I add other links too?
回答1:
Make sure to write the link inside the for loop. Using a with command save you for manually closing the file as well.
This 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'})
with open("BatchLinks.txt","w") as file:
for episode in subtitles:
x = episode.find_all('a')
for a in x:
z = a['href']
link = 'http://www.crunchyroll.com'+ z
print link
file.write(link)
回答2:
Just open the file first then write as you iterate:
with open(fa, "w") as f:
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'})
for episode in subtitles:
x = episode.find_all('a')
for a in x:
z = a['href']
f.write('http://www.crunchyroll.com{}\n'.format(z) )
Unless you want all links in the one line you need the \n at the end of the joined link. Your code would also write the last link not the first.
Output:
http://www.crunchyroll.com/i-cant-understand-what-my-husband-is-saying/episode-13-happy-days-678059
http://www.crunchyroll.com/i-cant-understand-what-my-husband-is-saying/episode-12-baby-skip-beat-678057
http://www.crunchyroll.com/i-cant-understand-what-my-husband-is-saying/episode-11-the-weight-of-value-and-the-value-of-weight-678055
http://www.crunchyroll.com/i-cant-understand-what-my-husband-is-saying/episode-10-fool-couple-678053
http://www.crunchyroll.com/i-cant-understand-what-my-husband-is-saying/episode-9-made-a-wish-it-came-true-and-i-got-in-trouble-678051
.........................
回答3:
The following code should allow you to write multiple lines to a file.
with open(fa, "w") as f:
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'})
for episode in subtitles:
x = episode.find_all('a')
for a in x:
z = a['href']
f.write('http://www.crunchyroll.com{}\n'.format(z) )
回答4:
https://docs.python.org/3/library/functions.html#open
'w' open for writing, truncating the file first
Each time you open the file with mode 'w' you truncate it, which means to discard any existing contents. You probably want to keep the file open until you are finished writing, then close it.
来源:https://stackoverflow.com/questions/31269177/how-to-write-multiple-lines-in-a-file-using-python