1

I am trying to save a json object that I scraped from a webpage into a csv file for geoprocessing. This is the code:

from bs4 import BeautifulSoup
import json
import urllib2
import csv
import os
import requests
import re

page1 = urllib2.urlopen("http://runkeeper.com/user/212579518/route/513771")
soup = BeautifulSoup(page1)

point_re = re.compile('.*routePoints =(.*);')
point_json = point_re.search(str(soup)).group(1)
point_data = json.loads(point_json)

##thislineworks
with open('test2.csv','wb') as f:
    w = csv.writer(f)
    w.writerows(point_data())

When I execute the code I got this message:

Traceback (most recent call last):
  File "C:\Users\Jesus\Desktop\scrapping1advance1.py", line 19, in <module>
    w.writerows(point_data())
TypeError: 'list' object is not callable

Any ideas on what i am doing wrong?

Thanks

1 Answer 1

1

Your error is with the code:

point_data = json.loads(point_json)

##thislineworks
with open('test2.csv','wb') as f:
    w = csv.writer(f)
    w.writerows(point_data())

point_data is not a function. Use:

point_data = json.loads(point_json)

##thislineworks
with open('test2.csv','wb') as f:
    w = csv.writer(f)
    w.writerows(point_data) #notice there are no parens here
Sign up to request clarification or add additional context in comments.

5 Comments

I tried the code you suggested but it returned this error: Traceback (most recent call last): File "C:\Users\Jesus\Desktop\scrapping1advance1.py", line 18, in <module> w.writerows(point_data) Error: sequence expected
The point_data = json.loads(point_json) is not assigning point_data to a sequence. This is a good time to use the python shell (such as IDLE) and run through your script in the shell, by typing in each line yourself. After you assign point_data, print it to the screen to see what kind of data it is. It may work if you change w.writerows to w.writerow. But I'm guessing that isn't what you intended.
When I change the code to write.row(point_json) it saves all the observations in a single row with the following format: {u'altitude': 40, u'longitude': -77.036478, u'deltaDistance': 0, u'latitude': 38.918704, u'type': u'StartPoint', u'deltaPause': 0} However I would like this ouput to look more like a dataframe. Any ideas on how to do this?
@jesusleal Yup. Thats what I meant by "guessing that isn't what you intended." Looking at the site you are pulling down, I think you may want to grab the entire json line, and not just one line? If so you will want to use the re.MULTILINE flag when creating your regex. Then you can use the writerows method
Thanks for your help. So I inspecte the object created and it is a list of dictionaries. With this code I got what I needed: keys = ['altitude','longitude','deltaDistance','latitude','type','deltaPause'] with open('test3.csv','wb') as f: dict_writer = csv.DictWriter(f, keys) dict_writer.writer.writerow(keys) dict_writer.writerows(point_data)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.