简体   繁体   English

Google趋势-配额限制-IP地址更改器

[英]Google Trends - Quota limit - IP address changer

I am writing a code using the Unofficial Google Trends API ( https://github.com/GeneralMills/pytrends#trend ), however after hardly 10 requests I have got the following error: Exceeded Google's Rate Limit. Please use time.sleep() to space requests. 我正在使用非官方Google趋势API( https://github.com/GeneralMills/pytrends#trend )编写代码,但是在几乎没有10个请求之后,我遇到了以下错误: Exceeded Google's Rate Limit. Please use time.sleep() to space requests. Exceeded Google's Rate Limit. Please use time.sleep() to space requests.

It seems that the following command does not properly connect to Google services. 以下命令似乎无法正确连接到Google服务。

  pytrends = TrendReq(google_username, google_password, custom_useragent=None)

Therefore I tried to change my IP address along with Tor Browser as explained here: https://stackoverflow.com/a/34516846/7110706 因此,我尝试按照以下说明更改与Tor浏览器一起的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)

There main issue is in the following code: 以下代码存在主要问题:

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

The code seems to work as the IP address changes over the time, however I am still stuck with Google request quota limit error: 该代码似乎可以随着IP地址的变化而工作,但是我仍然对Google请求配额限制错误感到困惑:

Exceeded Google's Rate Limit. 超过了Google的速率限制。 Please use time.sleep() to space requests. 请使用time.sleep()来分隔请求。

New IP address is : 178.217.187.39 新的IP地址是:178.217.187.39

Exceeded Google's Rate Limit. 超过了Google的速率限制。 Please use time.sleep() to space requests. 请使用time.sleep()来分隔请求。

New IP address is: 95.128.43.164 新的IP地址是:95.128.43.164

Do you know if there is a way to bypass the limitation? 您知道是否有办法绕过限制吗? Perhaps Google Trends does not get the new IP address as the request is not correctly routed by thor. 也许Google趋势无法获取新的IP地址,因为请求没有被正确地路由。

Thanks in advance. 提前致谢。

Have you already tried to (re)connect to Google inside the while loop? 您是否已尝试在while循环内(重新)连接到Google?

while mes == 0:
    pytrend = TrendReq(google_username, google_password, custom_useragent=None) # Connect to google
    try:
        results = pytrend.trend(trend_payload, return_type='dataframe').sort_index(axis=0, ascending=False) # Launch request in Google Trends
        mes = 1

UPDATE 1: As told me by the OP, my solution works only if a random user-agent is used. 更新1:如OP所言,仅当使用随机用户代理时,我的解决方案才有效。

Therefore something like the following code should work: 因此,类似以下代码的代码应该起作用:

def random_word(length):
"""Return a random word of 'length' letters."""
return ''.join(random.choice(string.ascii_letters) for i in range(length))

[...]

def requestDailydatafromGT(keywords, geography, date):  #parameters must be strings 
    [...]
    while mes == 0:
        pytrend = TrendReq(google_username, google_password, custom_useragent=random_word(8)) # Connect to Google
        try:
            results = pytrend.trend(trend_payload, return_type='dataframe').sort_index(axis=0, ascending=False) # Launch request in Google Trends
            mes = 1
    [...]

UPDATE 2: There is no need to authenticate every time you renew Tor. 更新2:每次续订Tor时都不需要进行身份验证。 You can simply do it once after the controller creation. 创建控制器后,只需执行一次即可。

controller = Controller.from_port(port=9051)
controller.authenticate(<YOUR_TOR_CONTROL_PASSWORD>)

As an additional information, standard ports should be: 作为附加信息,标准端口应为:

Tor: 9050 | Tor:9050 | Tor control: 9051 Tor控制:9051

Tor Browser: 9150 | Tor浏览器:9150 | Tor Browser control: 9151 Tor浏览器控件:9151

I used 9050 and 9051 ports after having un-commented "ControlPort 9051" in default Tor configuration file (and having added my hashed password). 在默认Tor配置文件中未注释“ ControlPort 9051”(并添加了我的哈希密码)后,我使用了9050和9051端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM