我正在尝试制作一个程序,可以通过python在youtube搜索中下载第一个链接。我正在使用bs4来抓取HTML和youtube-dl来下载第一个链接。出于某种原因,它没有超过os.system命令。它不会出现任何错误,只退出退出代码1。
这是我的代码(在Windows 10上使用python 2.7.1):
from bs4 import BeautifulSoup
import urllib2
import os
search = raw_input("What do you want to search?: ")
search2 = search.replace(" ", "+")
url = "https://www.youtube.com/results?search_query=" + search2 + "&page=&utm_source=opensearch"
content = urllib2.urlopen(url).read()
soup = BeautifulSoup(content)
for link in soup.find_all('a'):
a = link.get('href')
b = a[:6]
if (b == "/watch"):
print " "
print "First link: " + a
page = "youtube.com/" + a
command = "youtube-dl " + str(page)
os.system("cd C:/Python27/Scripts/" | command)
print "Done!"
break
print a