仅在csv的最后一行编写搜索结果

时间:2016-08-25 14:56:05

标签: python-2.7 csv

这是我的代码:

import urllib
import json
import csv

apiKey = "MY_KEY" # Google API credentials

##perform a text search based on input, place results in text-search-results.json
print "Starting"
myfile = open("results.csv","wb")
headers = []
headers.append(['Search','Name','Address','Phone','Website','Type','Google ID','Rating','Permanently Closed'])
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
wr.writerows(headers)
with open('input_file.csv', 'rb') as csvfile:
    filereader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in filereader:
    search = ', '.join(row)
    search.replace(' ', '+')
    url1 = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=%s&key=%s" % (search,apiKey)
    urllib.urlretrieve(url1,"text-search-results.json")

    print "SEARCH", search
    print "Google Place URL", url1

    ## load text-search-results.json and get the list of place IDs
    textSearchResults = json.load(open("text-search-results.json"))
    listOfPlaceIds = []
    for item in textSearchResults["results"]:
        listOfPlaceIds.append(str(item["place_id"]))

    ## open a nested list for the results
        output = []

        ## iterate through and download a JSON for each place ID
        for ids in listOfPlaceIds:
            url = "https://maps.googleapis.com/maps/api/place/details/json?placeid=%s&key=%s" % (ids,apiKey)
            fn = ids + "-details.json"
            urllib.urlretrieve(url,fn)

            data = json.load(open(fn))
            lineToAppend = []
            lineToAppend.append(search)

            try:
                lineToAppend.append(str(data["result"]["name"]))
            except KeyError:
                lineToAppend.append('')
            try:
                lineToAppend.append(str(data["result"]["formatted_address"]))
            except KeyError:
                lineToAppend.append('')
            try:
                lineToAppend.append(str(data["result"]["formatted_phone_number"]))
            except KeyError:
                lineToAppend.append('')
            try:
                lineToAppend.append(str(data["result"]["website"]))
            except KeyError:
                lineToAppend.append('')
            try:
                lineToAppend.append(str(data["result"]["types"]))
            except KeyError:
                lineToAppend.append('')
            try:
                lineToAppend.append(str(data["result"]["place_id"]))
            except KeyError:
                lineToAppend.append('')
            try:
                lineToAppend.append(str(data["result"]["rating"]))
            except KeyError:
                lineToAppend.append('')
            try:
                lineToAppend.append(str(data["result"]["permanently_closed"]))
            except KeyError:
                lineToAppend.append('')

        output.append(lineToAppend)
        wr.writerows(output)
myfile.close()

这样做是从input_file中的一列获取搜索字词,然后通过Google Places API运行该搜索。但是,当我有多个搜索词时,它只返回results.csv文件中的最后一个搜索结果。我不太清楚为什么会发生这种情况,因为它正在读取所有搜索术语并运行它们,但只返回最后的结果。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

目前您只写出最后一行,因为您正在重置lineToAppend循环中的变量for。但是,您不会在output循环中将其添加到for。因此,它会在for循环结束并写出最后一行。

因此目前它看起来像这样:(简称简称)

    for ids in listOfPlaceIds:
        url = "https://maps.googleapis.com/maps/api/place/details/json?placeid=%s&key=%s" % (ids,apiKey)
        fn = ids + "-details.json"
        urllib.urlretrieve(url,fn)

        data = json.load(open(fn))
        lineToAppend = []
        lineToAppend.append(search)

       ...

    output.append(lineToAppend)
    wr.writerows(output)

它应该是:

    for ids in listOfPlaceIds:
        url = "https://maps.googleapis.com/maps/api/place/details/json?placeid=%s&key=%s" % (ids,apiKey)
        fn = ids + "-details.json"
        urllib.urlretrieve(url,fn)

        data = json.load(open(fn))
        lineToAppend = []
        lineToAppend.append(search)

       ...

        output.append(lineToAppend)
    wr.writerows(output)