スプレッドシートにまとめてあるキーワードを取得し、MEOの順位をスクレイピングで調べ、スプレッドシートに記入するというコードを書いています。
現在起きているエラーなのですが、調べたところ、gspread.exceptions.APIErrorの"code": 500は原因を特定できるヒントがほぼないということを示しているとのことでした。
色々試しても解決できないのですが、よくある原因にはどんなことが挙げられるのでしょうか。
参考になるかはわかりませんが、コードをほぼ全て乗せておきます。
python
1scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive'] 2credentials = ServiceAccountCredentials.from_json_keyfile_name('', scope) 3gc = gspread.authorize(credentials) 4 5Gi = 0 6def gmb(): 7 global Gi 8 Gi += 1 9 #スプレッドシートにまとまってある各シートのIDを取得 10 SPREADSHEET_TITLE = '' 11 wb = gc.open(SPREADSHEET_TITLE) 12 ws_master = wb.get_worksheet(0) 13 14 sheets = ws_master.col_values(4) 15 sheets = sheets[5:] 16 sheet_list_sub = [] 17 for sheet in sheets: 18 sheet = sheet.lstrip('https://docs.google.com/spreadsheets/d/') 19 sheet = sheet.split('/edit') 20 sheet_id = sheet[0] 21 sheet_list_sub.append(sheet_id) 22 sheet_list = random.sample(sheet_list_sub, len(sheet_list_sub)) 23 24 25 for ID in sheet_list: 26 #シートごとにまとまっているキーワードを取得 27 SPREADSHEET_KEY = ID 28 wb = gc.open_by_key(SPREADSHEET_KEY) 29 ws_lists = wb.worksheets() 30 31 for j in range(len(ws_lists)): 32 ws1 = wb.get_worksheet(j) 33 34 try: 35 find_date = ws1.find('Date.') 36 except CellNotFound: 37 continue 38 39 keywords = ws1.row_values(find_date.row) 40 keywords = keywords[6:] 41 title_name = ws1.acell('B2').value 42 print(title_name) 43 44 option = Options() 45 option.add_argument('--incognito') 46 option.add_argument("--disable-gpu") 47 option.add_argument('--headless') 48 browser = webdriver.Chrome(options=option) 49 50 place = ws1.cell(find_date.row-1,4,).value 51 places = ws1.cell(find_date.row-1,5).value 52 53 places = places.split(',') 54 55 #検索するときは位置情報を変更 56 browser.execute_cdp_cmd( 57 "Browser.grantPermissions", 58 { 59 "origin": "https://www.google.com/", 60 "permissions": ["geolocation"] 61 }, 62 ) 63 64 browser.execute_cdp_cmd( 65 "Emulation.setGeolocationOverride", 66 { 67 "latitude": float(places[0]), 68 "longitude": float(places[1]), 69 "accuracy": 100, 70 }, 71 ) 72 #記入日を入力 73 dt_now = datetime.datetime.now() 74 75 date_col = ws1.col_values(2) 76 date = dt_now.strftime('%m/%d') 77 row = len(date_col)+1 78 ws1.update_acell('B'+str(row), date) 79 time = dt_now.strftime('%H:%M:%S') 80 ws1.update_acell('C'+str(row),time) 81 ws1.update_acell('D'+str(row),place) 82 83 #キーワードを検索 84 for keyword in keywords: 85 browser.get('https://www.google.com/') 86 gmb_name = browser.find_element_by_class_name('gLFyf') 87 gmb_name.send_keys(keyword) 88 gmb_name.send_keys(Keys.ENTER) 89 sleep(2) 90 91 allel = browser.find_elements_by_class_name('ndElDd') 92 if len(allel) > 0: 93 aTag = allel[0].find_element_by_tag_name("a") 94 u = aTag.get_attribute("href") 95 browser.get(u) 96 sleep(2) 97 98 names = [] 99 count = 0 100 urls = [] 101 102 #順位を計算 103 for i in range(5): 104 sleep(10) 105 results = browser.find_elements_by_class_name('VkpGBb') 106 for result in results: 107 ads = result.find_elements_by_class_name('VqFMTc') 108 if len(ads) > 0: 109 continue 110 count += 1 111 name = result.find_element_by_class_name('dbg0pd') 112 if title_name == name.text: 113 break 114 else: 115 try: 116 pages = browser.find_element_by_id('pnnext') 117 pages.click() 118 if i == 4: 119 count = 'ー' 120 continue 121 except NoSuchElementException: 122 break 123 except: 124 break 125 126 break 127 128 cell = ws1.find(keyword) 129 ws1.update_cell(row,cell.col,count) 130 131 else: 132 gmb_title = browser.find_elements_by_class_name('qrShPb') 133 if len(gmb_title) > 0: 134 count = 1 135 cell = ws1.find(keyword) 136 ws1.update_cell(row,cell.col,count) 137 else: 138 count = 'ー' 139 cell = ws1.find(keyword) 140 ws1.update_cell(row,cell.col,count) 141 142 browser.quit() 143 144#記入したい時間帯を入力してもらう 145times = [] 146while True: 147 t = input('時間:') 148 if t == 'y': 149 break 150 else: 151 times.append(t) 152 153#時間になったら実行 154for ti in times: 155 schedule.every().day.at(ti).do(gmb) 156 157while True: 158 schedule.run_pending() 159 sleep(1) 160 if Gi == len(times): 161 print('finish') 162 break
あなたの回答
tips
プレビュー