hdr = {'User-Agent': 'Mozilla/5.0'}
url = "https://www.youtube.com/results?search_query=%s+%s" % (artistName, songName)
req = urllib2.Request(url, headers=hdr)
page = urllib2.urlopen(req)
soup = BeautifulSoup(page)
for SongTitle in soup.findAll('a', {'class': 'yt-uix-tile-link yt-ui-ellipsis yt-ui-ellipsis-2 yt-uix-sessionlink spf-link '}):
song_titles.append(SongTitle.string)
i += 1
所以这是我用来搜索带有自定义 URL 的 youtube 页面的代码。之后,用户可以根据视频编号选择下载哪个视频。问题是,我尝试下载的大多数视频都返回以下错误:
WARNING:root:ciphertag doesn't match signature type
WARNING:root:JRMOMjCoR58
Traceback (most recent call last):
File "PyKo.py", line 123, in <module>
query(sys.argv[1], sys.argv[2])
File "PyKo.py", line 118, in query
downloadSong(link_to_download, s_url)
File "PyKo.py", line 30, in downloadSong
video = pafy.new(url)
File "C:\Python27\lib\site-packages\pafy\pafy.py", line 138, in new
return Pafy(url, basic, gdata, signature, size, callback)
File "C:\Python27\lib\site-packages\pafy\pafy.py", line 1041, in __init__
self.fetch_basic()
File "C:\Python27\lib\site-packages\pafy\pafy.py", line 1087, in fetch_basic
self.dash = _extract_dash(self._dashurl)
File "C:\Python27\lib\site-packages\pafy\pafy.py", line 274, in _extract_dash
dashdata = fetch_decode(dashurl)
File "C:\Python27\lib\site-packages\pafy\pafy.py", line 91, in fetch_decode
req = g.opener.open(url)
File "C:\Python27\lib\urllib2.py", line 410, in open
response = meth(req, response)
File "C:\Python27\lib\urllib2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python27\lib\urllib2.py", line 448, in error
return self._call_chain(*args)
File "C:\Python27\lib\urllib2.py", line 382, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 531, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 403: Forbidden
并且只有非常少量的视频有效。我用谷歌搜索并找到了类似问题的答案,它说要插入一个标题,所以我做了,但它仍然不起作用。提前致谢!