次のコードから、Google 検索エンジンからの曲名 (ファイル "playlist.txt" の行) を含む各行を照会し、その結果に基づいて YouTube リンクを取得し、youtube- dl、オーディオを抽出しています。
結果が保持するリンクを実行すると、GOOGLE はコンピュータ システムからの異常なトラフィックを主張することをブロックします。
リクエスト間の時間経過を維持するためにスリープを使用しました。
そのエラーを修正する方法を知りたい
ユーザー エージェントを変更するか、プロキシを使用すると、自動検索検出を回避するのに役立つと思います。それを実現するためにコードを変更する方法を知りたかったのです
import urllib
import json as m_json
import re
import time
import subprocess
from random import randint
import getpass
playlist=open('playlist.txt','r')
songs = playlist.readlines()
song_num = 1
for song in songs:
query = song
query = urllib.urlencode ( { 'q' : query } )
response = urllib.urlopen ( 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&' + query ).read()
json = m_json.loads ( response )
results = json [ 'responseData' ] [ 'results' ]
for result in results:
title = result['title']
url = result['url']
if re.search(r'www.youtube.com',url):
print ( title + '; ' + url )
print "DOWNLOADING",title
decoded_url=urllib.unquote(url).decode('utf8')
print decoded_url
subprocess.call(['youtube-dl','-o','/home/'+getpass.getuser()+'/Videos/playlist%('+title+").(ext)s","--extract-audio","--audio-format","mp3",decoded_url])
break;
print song_num
time.sleep(randint(10,15))
song_num+=1
出力
DOWNLOADING <b>Black Sabbath Iron Man</b> - YouTube
http://www.youtube.com/watch?v=rT4KpfiFcNc
[youtube] rT4KpfiFcNc: Downloading webpage
[youtube] rT4KpfiFcNc: Extracting video information
[youtube] rT4KpfiFcNc: Downloading DASH manifest
ERROR: Error in output template: unsupported format character '(' (0x28) at index 73 (encoding: 'UTF-8')