-1

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
  • Open the file before your for episode in subtitles loop, write l plus a newline char (if you want it) each time through the loop, then close the file once the loop exits. Commented Jul 7, 2015 at 13:01

4 Answers 4

3

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)
Sign up to request clarification or add additional context in comments.

Comments

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
.........................

1 Comment

Change fa to whatever name you want to save the file as
2

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) )

2 Comments

Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem. It's also recommended that you don't post an answer if it's just a guess. A good answer will have a plausible reason for why it could solve the OP's issue.
@SuperBiasedMan , ok dude will consider that next time <3
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.