Using loop(Efficient Looping technique): To extract certain url's out of my string in Python
<p>The following lines of code gives me the source code to a specific playlist and stores all url's in a variable "newlink". I wish to write a loop that can go through this string of url's and store the ones that say '/watch?v=' into an array in Python so that array[0] would give me the first link, array[1] the second and so on. What would be the best way of doing this?</p>

<pre><code>import re
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import requests


#Asks which playlist you want downloaded
print ('Which playlist do you want to download?')
playlist = input()

#Access my youtube playlists page
driver = webdriver.Chrome(executable_path='E:\chromedriver\chromedriver.exe')
driver.get("https://www.youtube.com/user/randomuser/playlists?sort=dd&amp;view=1&amp;shelf_id=0")

#Access the 'classic' playlist
if playlist == 'classic':
    driver.find_element_by_xpath('//a[contains(text(), "classic")]').click()
    newurl = driver.current_url

    requrl = requests.get(newurl)
    requrlcont = requrl.content

    soup = BeautifulSoup(requrlcont, "html.parser")
    for link in soup.find_all('a'):
        newlink = link.get('href'))
</code></pre>

<p>This code is tested with a playlist that actually created the array. </p>

<pre><code>import re
from selenium import webdriver
from bs4 import BeautifulSoup
import requests

#Asks which playlist you want downloaded
print ('Which playlist do you want to download?')
playlist = raw_input()

#Access my youtube playlists page
driver = webdriver.Chrome(executable_path='/usr/lib/chromium-browser/chromedriver')
driver.get("https://www.youtube.com/user/randomuser/playlists?sort=dd&amp;view=1&amp;shelf_id=0")

#Access the 'Favorites' playlist
if playlist == 'Favorites':
    driver.find_element_by_xpath('//a[contains(text(), "Favorites")]').click()
    newurl = driver.current_url

    requrl = requests.get(newurl)
    requrlcont = requrl.content
    links = []
    soup = BeautifulSoup(requrlcont, "html.parser")
    for link in soup.find_all('a'):
        #print("link " + str(link))
        if re.match("/watch\?v=", link.get('href')):
            links.append(link.get('href'))

    print links
</code></pre>

<p>Output</p>

<pre><code>python pyprog.py 
Which playlist do you want to download?
Favorites
[u'/watch?v=fG9_AYzehJw&amp;list=FLRMDL-xDn7xqDznwaQbUH6g', u'/watch?v=fG9_AYzehJw&amp;list=FLRMDL-xDn7xqDznwaQbUH6g', u'/watch?v=fG9_AYzehJw&amp;index=1&amp;list=FLRMDL-xDn7xqDznwaQbUH6g', u'/watch?v=fG9_AYzehJw&amp;index=1&amp;list=FLRMDL-xDn7xqDznwaQbUH6g', u'/watch?v=MKfDwChOoHI&amp;index=2&amp;list=FLRMDL-xDn7xqDznwaQbUH6g', u'/watch?v=MKfDwChOoHI&amp;index=2&amp;list=FLRMDL-xDn7xqDznwaQbUH6g', u'/watch?v=JQTXawaAKNA&amp;index=3&amp;list=FLRMDL-xDn7xqDznwaQbUH6g', u'/watch?v=JQTXawaAKNA&amp;index=3&amp;list=FLRMDL-xDn7xqDznwaQbUH6g', u'/watch?v=dG8wsae-6tU&amp;index=4&amp;list=FLRMDL-xDn7xqDznwaQbUH6g', u'/watch?v=dG8wsae-6tU&amp;index=4&amp;list=FLRMDL-xDn7xqDznwaQbUH6g', u'/watch?v=9mcZdDCOeuE&amp;index=5&amp;list=FLRMDL-xDn7xqDznwaQbUH6g', u'/watch?v=9mcZdDCOeuE&amp;index=5&amp;list=FLRMDL-xDn7xqDznwaQbUH6g', u'/watch?v=lh0ZB9OD_fg&amp;index=6&amp;list=FLRMDL-xDn7xqDznwaQbUH6g', u'/watch?v=lh0ZB9OD_fg&amp;index=6&amp;list=FLRMDL-xDn7xqDznwaQbUH6g', u'/watch?v=NfLmCPfx_gY&amp;index=7&amp;list=FLRMDL-xDn7xqDznwaQbUH6g', u'/watch?v=NfLmCPfx_gY&amp;index=7&amp;list=FLRMDL-xDn7xqDznwaQbUH6g', u'/watch?v=FoQzWb_f1oA&amp;index=8&amp;list=FLRMDL-xDn7xqDznwaQbUH6g', u'/watch?v=FoQzWb_f1oA&amp;index=8&amp;list=FLRMDL-xDn7xqDznwaQbUH6g', u'/watch?v=l8rJ1WML60Y&amp;index=9&amp;list=FLRMDL-xDn7xqDznwaQbUH6g', u'/watch?v=l8rJ1WML60Y&amp;index=9&amp;list=FLRMDL-xDn7xqDznwaQbUH6g', u'/watch?v=9rCug1ANQdE&amp;index=10&amp;list=FLRMDL-xDn7xqDznwaQbUH6g', u'/watch?v=9rCug1ANQdE&amp;index=10&amp;list=FLRMDL-xDn7xqDznwaQbUH6g']
</code></pre>

python