I’m having trouble looping through a list and visiting urls (with selenium) - but this is a basic python problem. I’m still learning.
I’ve imported a Google Sheet using the service api. following this tutorial:
----- code --------
from googleapiclient.discovery import build
from google.oauth2 import service_account
import pandas as pd
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
SERVICE_ACCOUNT_FILE = ‘keys.json’
SCOPES = [‘https://www.googleapis.com/auth/spreadsheets’]
creds = None
creds = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
The ID and range of a sample spreadsheet.
SPREADSHEET_ID = ‘1dvHeynsaQr9vM46K2HuxhaiUX3G7DMt04OA’
service = build(‘sheets’, ‘v4’, credentials=creds)
Call the Sheets API
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=SPREADSHEET_ID,
range=“Sheet1!A3:A”).execute()
values = result.get(‘values’, )
df = pd.DataFrame(values)
print(“”)
print(values)
print(“”)
print(df)
—result—
[['https://bobscarpetandtilecare.com/'], ['https://hydrocleaninc.com/'], ['https://carpetsbygeorgeco.com/']] 0 0 https://bobscarpetandtilecare.com/ 1 https://hydrocleaninc.com/ 2 https://carpetsbygeorgeco.com/------ code ----
I need to loop through the dataframe, and visit each url, so something like this:
for row in df.iterrows():
s = Service(‘/usr/local/bin/chromedriver’)
driver = webdriver.Chrome(service=s)
driver.get(url)
time.sleep(3)
print(“success”)
driver.quit()
But I can’t because it’s telling me the json data is not serializable:
TypeError: Object of type Series is not JSON serializable
What is the correct way to loop through and visit the urls on this list?