Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are looping through every img tag in this line and printing each one:</p> <pre><code>for link in soup.find_all('img'): print link.get('src') </code></pre> <p>However, you are not writing to the file in that loop, you are just writing <code>link.get('src')+'\n'</code> at the very end.</p> <p>This will only write what link is currently assigned to, which is simply the <strong>last</strong> img tag that you found in your loop above. That is why only one 'src' value will be written to the output file.</p> <p>You need to be writing each line to the file within your loop that goes through each of the img tags that you are interested in. You'll need to do a little bit of rearranging to do that:</p> <pre><code>from bs4 import BeautifulSoup soup = BeautifulSoup(open("youtube.htm")) file = open("parseddata.txt", "wb") for link in soup.find_all('img'): print link.get('src') file.write(link.get('src')+"\n") file.flush() file.close() </code></pre> <p>You should also remember to close the file as I have added in the last line of the above snippet.</p> <p>Edit: As per Hooked's comment below, here is what this snippet would look like if you use the <code>with</code> keyword. Using <code>with</code> will close the file automatically for you as soon as the indented block ends, so that you don't even have to think about it:</p> <pre><code>from bs4 import BeautifulSoup soup = BeautifulSoup(open("youtube.htm")) with open("parseddata.txt", "wb") as file: for link in soup.find_all('img'): print link.get('src') file.write(link.get('src')+"\n") </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload