【问题标题】:Getting google "I'm not a robot" option after consecutive searches [duplicate]连续搜索后获得谷歌“我不是机器人”选项[重复]
【发布时间】:2020-05-09 02:03:25
【问题描述】:

我有一个使用 selenium 和 Python 从 URL 获取详细信息的工作代码。 但是在搜索 50 多个 URL 后遇到问题,谷歌浏览器显示“我不是机器人”选项并要求选中该复选框。

但在那之后无法获得结果,此后没有显示一致的结果或错误的结果。

那么有没有办法避免这种“我不是机器人”的验证码并获得一致的结果? 或者我需要在此代码中修改什么以使其更优化?

也可以在chrome驱动中同时打开50个或100个标签页,并在加载的标签页中搜索结果吗?

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import psycopg2
import os
import datetime

final_results=[]
positions=[]

option = webdriver.ChromeOptions()
option.add_argument("—-incognito")
browser = webdriver.Chrome(executable_path='/users/user_123/downloads/chrome_driver/chromedriver', chrome_options=option)

#def db_connect():
try:
    #Database connection string
     DSN = "dbname='postgres' user='postgres' host='localhost' password='postgres' port='5432'"
     #DWH table to which data is ported
     TABLE_NAME = 'staging.search_url'
     #Connecting DB..
     conn = psycopg2.connect(DSN)
     print("Database connected...")
     #conn.set_client_encoding('utf-8')
     cur = conn.cursor()
     cur.execute("SET datestyle='German'")
except (Exception, psycopg2.Error) as error:
     print('database connection failed')
     quit()

def get_products(url):
    browser.get(url)
    names = browser.find_elements_by_xpath("//span[@class='pymv4e']")
    upd_product_name_list=list(filter(None, names))
    product_name = [x.text for x in upd_product_name_list]
    product = [x for x in product_name if len(x.strip()) > 2]
    upd_product_name_list.clear()
    product_name.clear()
    return product

##################################
search_url_fetch="""select url_to_be_searched from staging.search_url where id in(65,66,67,68)"""
psql_cursor = conn.cursor()
psql_cursor.execute(search_url_fetch)
serach_url_list = psql_cursor.fetchall()
print('Fetched DB values')
##################################

for row in serach_url_list:
    passed_url=''
    passed_url=str(row)
    passed_url=passed_url.replace(',)','')
    passed_url=passed_url.replace('(','')
    new_url=''
    new_url=passed_url[1:len(passed_url)-1]
    print('Passed URL :'+new_url)
    print("\n")

    filtered=[]
    filtered.clear()
    filtered = get_products(new_url)
    if not filtered:
        new_url=new_url+'+kaufen'
        get_products(new_url)
        print('Modified URL :'+new_url)

    if filtered:
         print(filtered)
         positions.clear()
         for x in range(1, len(filtered)+1):
           positions.append(str(x))
         gobal_position=0
         gobal_position=len(positions)
         print('global postion first: '+str(gobal_position))
         print("\n")

         company_name_list = browser.find_elements_by_xpath("//div[@class='LbUacb']")
         # use list comprehension to get the actual repo titles and not the selenium objects.
         company = []
         company.clear()
         company = [x.text for x in company_name_list]
         # print out all the titles.
         print('Company Name:')
         print(company, '\n')


         price_list = browser.find_elements_by_xpath("//div[@class='e10twf T4OwTb']")
         # use list comprehension to get the actual repo titles and not the selenium objects.
         price = []
         price.clear()
         price = [x.text for x in price_list]
         print('Price:')
         print(price)
         print("\n")

         urls=[]
         urls.clear()
         find_href = browser.find_elements_by_xpath("//a[@class='plantl pla-unit-single-clickable-target clickable-card']")
         for my_href in find_href:
             url_list=my_href.get_attribute("href")
             urls.append(url_list)
             #print(my_href.get_attribute("href"))
         print('URLS:')
         print(urls)
         print("\n")

         print('Final Result: ')
         result = zip(positions,filtered, urls, company,price)
         final_results.clear()
         final_results.append(tuple(result))
         print(final_results)
         print("\n")


         print('global postion end :'+str(gobal_position))
         i=0
         #try:
         for d in final_results:
                print( d[i])
                while i <= gobal_position:
                  cur.execute("""INSERT into staging.pla_crawler_results(position, product_name, url,company,price) VALUES (%s, %s, %s,%s, %s)""", d[i])
                  print('Inserted succesfully')
                  conn.commit()
                  i=i+1
         #except (Exception, psycopg2.Error) as error:
             #pass

【问题讨论】:

  • 我不确定这是一个可以回答的问题——正如 afarley 所说,任何答案(除了使用谷歌认可的方法,大量需要付钱给他们)正在进入军备竞赛,最终会破裂。
  • 可能不会。打开许多标签将无济于事。可能每 x 次搜索重新启动驱动程序?不过,绕过验证码似乎对我有点敌意……做一个优秀的互联网公民™
  • ...请记住,SO 的目标是成为一个巨型/长尾常见问题解答。本月有效并在下个月中断的东西并不是真正合适的常见问题解答条目。事实上,有人可能会争辩说,随着范围将不断扩大以包含新的反措施和反措施,它天生就“太宽泛”而无法回答。

标签: python python-3.x selenium selenium-chromedriver


【解决方案1】:

你有两个选择:

1) 为访问 Google 的搜索 API 付费。这是避免被禁止的专业方法。

2) 随机化您的脚本,使其看起来更人性化。这种方法是与谷歌的军备竞赛;您可能可以让您的脚本正常工作,但它会定期中断。

更多优化(在性能方面)可能会使这个问题变得更糟,而不是更好。

【讨论】:

    【解决方案2】:

    用你的代码尝试的事情:

    1. 用头部启动 Chrome。看起来您使用的不是 headless,而是那里的提醒。
    2. 随机化与网页交互的时间。获得自动检查的最快方法是加快速度。
    3. 排除enable-automation参数;见https://stackoverflow.com/a/56635123/9642

    替代方案:

    afarley 建议为 Google 的 Search API 付费,但我不知道这甚至是一种选择。 Google 曾经为前 10 个结果提供免费的基本搜索 API(具有严格的配额限制),但我认为不再提供。

    【讨论】:

      猜你喜欢
      • 2018-05-06
      • 2020-11-29
      • 1970-01-01
      • 1970-01-01
      • 2019-12-30
      • 2012-08-19
      • 2020-04-18
      • 2016-10-03
      • 2012-01-04
      相关资源
      最近更新 更多