非公式の Google トレンド API ( https://github.com/GeneralMills/pytrends#trend )を使用してコードを書いていますが、ほとんど 10 回のリクエストを行った後、次のエラーが発生しました。Exceeded Google's Rate Limit. Please use time.sleep() to space requests.
以下のコマンドでは、Google サービスに正しく接続されていないようです。
pytrends = TrendReq(google_username, google_password, custom_useragent=None)
したがって、ここで説明されているように、Tor Browser とともに IP アドレスを変更しようとしました: https://stackoverflow.com/a/34516846/7110706
controller = Controller.from_port(port=9151)
def connectTor():
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5 , "127.0.0.1", 9150, True)
socket.socket = socks.socksocket
def renew_tor():
controller.authenticate()
controller.signal(Signal.NEWNYM)
def showmyip():
url = "http://www.showmyip.gr/"
r = requests.Session()
page = r.get(url)
soup = BeautifulSoup(page.content, "lxml")
ip_address = soup.find("span",{"class":"ip_address"}).text.strip()
print('New IP adress is:' + ip_address)
主な問題は次のコードにあります。
def requestDailydatafromGT(keywords, geography, date): #parameters must be strings
from pytrends.request import TrendReq
import time
from random import randint
google_username = "" #put your gmail account
google_password = ""
path = ""
#Connect to google
pytrend = TrendReq(google_username, google_password, custom_useragent=None)
requestdate=str(date)+' 3m'
trend_payload = {'q': keywords,'hl': 'en-US','geo': geography, 'date': requestdate} #define parameters of the request
mes=0
while mes==0:
try:
results= pytrend.trend(trend_payload, return_type='dataframe').sort_index(axis=0, ascending=False) #launch request in Google tren0ds
mes=1
except Exception:
renew_tor()
connectTor()
time.sleep(randint(5,15))
mes=0
return results
時間の経過とともに IP アドレスが変更されると、コードは機能するように見えますが、Google リクエストのクォータ制限エラーが引き続き発生します。
Google のレート制限を超えました。time.sleep() を使用してリクエストをスペースしてください。
新しい IP アドレス: 178.217.187.39
Google のレート制限を超えました。time.sleep() を使用してリクエストをスペースしてください。
新しい IP アドレス: 95.128.43.164
制限を回避する方法があるかどうか知っていますか? トールによってリクエストが正しくルーティングされないため、Google トレンドが新しい IP アドレスを取得できない可能性があります。
前もって感謝します。