learner
learner

Reputation: 4818

How to download using wget one by one in a loop in python

I have following code written which downloads from a page:

import urllib2,os
    from bs4 import BeautifulSoup

    page = urllib2.urlopen('http://somesite.com')
    soup = BeautifulSoup(page)

    for a in soup.find_all('a', href=True):
        if "tutorials" in a['href']:
            os.system('wget ' + a['href'])

The issue is above command is it downloads all links altogether from the page. I want to download videos one by one. Is that possible? Please help me.

Upvotes: 1

Views: 1744

Answers (2)

Rohit Babu
Rohit Babu

Reputation: 410

I'm quite late here but I think this might help someone. How about using os.system() in a separate function and calling it every time in a loop?

like:

def helper(arg):
   if(os.system(arg) == 0):
        print(arg," completed")
        return None

and

for link in links:
    helper("wget "+link)

I think this will ensure the one-by-one download. Correct me if I am wrong. Thanks!

Upvotes: 1

Tobey
Tobey

Reputation: 1440

Use the subprocess.call module instead of os.system.

subprocess.call(['wget' , a['href']])

Upvotes: 3

Related Questions