Problems getting url. TypeError: Object of type Series is not JSON serializable

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?

Welcome!

You’re doing some relatively fancy stuff—I wouldn’t call that basic!

If you don’t understand what’s going on, it might be a better idea to start with simpler code that you do understand, get familiar with it, and then move on to more complex tasks. You’ll learn a lot more and be frustrated a lot less if that’s the case.

In any event, its unclear what is going on, since you haven’t provided the full error traceback, which is vital for proper debugging, nor stated what specific line(s) of the code you’ve shown is being cited there. If I were to hazard a guess, though, you’re trying to pass a pandas.Series, i.e. a pandas Series object (a single-column dataframe, more or less) to json.dump/json.dumps, which is not going to work since json doesn’t know how to convert arbitrary objects, like pandas.Series, to JSON.

Fortunately, Series itself does know how to convert itself to JSON—you need its to_json method. You might need to play with the options to get the data in the format you’re expecting, and there’s no guarantee that will fix all your issues, but it should resolve your proximate one.

Beyond that, I do want to note that you’re looking through row of df, but then not using it, and rather using url which I don’t see you define anywhere. You could just look over the specific dataframe column that contains your URLs. Again, I can’t provide much useful advice since you don’t define the schema for your dataframe, how it was created, or a sample of it, so you’ll have to figure that out on your own.

Also, please format your code correctly, so we can actually read it—indentation is critical in Python. Copy and paste the following:

```python
# YOUR CODE HERE
```

It should look like this; use the handy preview to the right to check that it does.

# YOUR CODE HERE

Best of luck!