我正在尝试开发一个可以从Youtube上下载视频和声音轨道的应用程序,但有时它会在查询“harry potter”或“the of the rings”之后弹出链接所显示的TypeError。 TypeError Message
这是我的python代码:
from __future__ import unicode_literals
import requests
from bs4 import BeautifulSoup
import youtube_dl
def find_search_content(search):
request = requests.get("https://www.youtube.com/results?search_query={}".format(search))
content = request.content
soup = BeautifulSoup(content, "html.parser")
return soup
def find_page_content(search):
request = requests.get("https://www.youtube.com/results?{}".format(search))
content = request.content
soup = BeautifulSoup(content, "html.parser")
return soup
def find_video(soup, all_item, i=1):
for element in soup.find_all('a', {"rel": "spf-prefetch"}):
video_title = element.get('title')
video_link = element.get('href')
img_value = element.get('href').split("=")[1]
img = "https://i.ytimg.com/vi/{}/hqdefault.jpg".format(img_value)
all_item['{}'.format(i)] = {"title": video_title, "link": "https://www.youtube.com{}".format(video_link),
'img': img}
i += 1
return all_item
def video_time(soup, all_item, i=1):
for time in soup.find_all('span', {"class": "video-time"}):
all_item.get('{}'.format(i))['time'] = time.text
i += 1
return all_item
def every_video(soup):
all_item = {}
find_video(soup, all_item, i=1)
video_time(soup, all_item, i=1)
return all_item
非常感谢!