Python 为什么该代码不打印所有可用业务的评论?

Python 为什么该代码不打印所有可用业务的评论?,python,json,csv,google-api,python-requests,Python,Json,Csv,Google Api,Python Requests,我正试图收集所有可用的谷歌商业评论。举个例子,佐治亚州有10多位泌尿科医生在谷歌评论中列出。但是,当我运行这段代码时,它只在csv文件中提供4-5名泌尿科医生的信息。但是,我想要谷歌评论中列出的所有至少有一个评级/评论的企业的信息。在此代码中应该做哪些更改? 谢谢 因为从内部循环中的len(reviews)中减去1,所以跳过了上一次审阅。如果一个泌尿科医生只有一次回顾,你将完全跳过该泌尿科医生。因此,您只需将有超过1篇评论的泌尿科医生放入您的CSV文件中 在两个for循环中都去掉-1。但是我建议

我正试图收集所有可用的谷歌商业评论。举个例子,佐治亚州有10多位泌尿科医生在谷歌评论中列出。但是,当我运行这段代码时,它只在csv文件中提供4-5名泌尿科医生的信息。但是,我想要谷歌评论中列出的所有至少有一个评级/评论的企业的信息。在此代码中应该做哪些更改? 谢谢


因为从内部循环中的
len(reviews)
中减去1,所以跳过了上一次审阅。如果一个泌尿科医生只有一次回顾,你将完全跳过该泌尿科医生。因此,您只需将有超过1篇评论的泌尿科医生放入您的CSV文件中

在两个
for
循环中都去掉
-1
。但是我建议您在设置
place\u id
时,为列表中的项更改为更具Python风格的
:语法和列表理解

由于某些评论没有
review\u url
属性,因此需要提供默认值。您可以使用
review.get('author\u url','')来完成此操作


为什么要从正在循环的列表长度中减去1?循环列表的python方法是对列表中的项使用
,而不是对范围内的i使用
(len(list)):
@Barmar,这样它就不会超出范围。
range(n)
返回一个从
0
n-1
的列表。它已经为你减去了1,你不需要自己去做。你看过返回的JSON了吗,看看它是否有超过4-5个结果?我按照你说的做了更改。现在,它只打印了很少的结果(比以前少),并给出了这个错误:
KeyError:'author\u url'
。这意味着一些评论没有
author\u url
字段。您需要对此进行检查并提供默认值。
import requests
import csv
import pprint

#sending get request.
main_api = "https://maps.googleapis.com/maps/api/place/textsearch/json?"
parameters = {"query":"Urologists, Georgia",
            "key":" "} #enter api key here.
resp = requests.get(main_api, parameters).json()


#it selects the places with at least one rating, and puts their place id in place_id.
place_id = []
for i in range(len(resp['results'])-1):
    if 'rating' in resp['results'][i]:
        place_id.append(resp['results'][i]['place_id'])

#creating a csv file and with headings.
with open("Urologists_FunGeorgia_Google.csv", "w") as toWrite:
    writer=csv.writer(toWrite)
    writer.writerow(['Date Collected',  'Health Care Provider', 'HCP location', 'Website Review is From', 'Specialty', 'Reviewer Name',\
        'Date of Review', 'Reviewer Demographics(gender/race)', 'Star Rating', 'How Many Stars', 'Other Meta-Data', 'Review', 'URL'])
    #getting responses using place ids collected in place_id.
    for ids in place_id:
        details_api = "https://maps.googleapis.com/maps/api/place/details/json?"
        parameters = {"placeid": ids,
                    "key":" " } #api key here.
        detail_resp = requests.get(details_api, parameters)
        resp1 = detail_resp.json()
        reviewss = resp1['result']['reviews']
        doc_name=resp1['result']['name']
        doc_url = resp1['result']['url']
        city_state = resp1['result']['formatted_address']
        website = 'GOOGLE'
        specialty = 'Urologists'
        date_collected = 'June 15 2017'
        total_poss = '5'
        #gets multiple reviews of the physician(if any).
        for i in range(len(reviewss)-1):
            rating = resp1['result']['reviews'][i]['rating']
            revname = resp1['result']['reviews'][i]['author_name']
            rev = resp1['result']['reviews'][i]['text']
            date_review = resp1['result']['reviews'][i]['relative_time_description']
            rev_url = resp1['result']['reviews'][i]['author_url']

            writer.writerow([date_collected, doc_name, city_state, website, specialty, revname, date_review, rev_url, rating, total_poss, '', rev, doc_url])
import requests
import csv
import pprint

#sending get request.
main_api = "https://maps.googleapis.com/maps/api/place/textsearch/json?"
parameters = {"query":"Urologists, Georgia",
            "key":" "} #enter api key here.
resp = requests.get(main_api, parameters).json()

#it selects the places with at least one rating, and puts their place id in place_id.
place_id = [result['place_id'] for result in resp['results'] if 'rating' in result]

#creating a csv file and with headings.
with open("Urologists_FunGeorgia_Google.csv", "w") as toWrite:
    writer=csv.writer(toWrite)
    writer.writerow(['Date Collected',  'Health Care Provider', 'HCP location', 'Website Review is From', 'Specialty', 'Reviewer Name',\
        'Date of Review', 'Reviewer Demographics(gender/race)', 'Star Rating', 'How Many Stars', 'Other Meta-Data', 'Review', 'URL'])
    #getting responses using place ids collected in place_id.
    for ids in place_id:
        details_api = "https://maps.googleapis.com/maps/api/place/details/json?"
        parameters = {"placeid": ids,
                    "key":" " } #api key here.
        detail_resp = requests.get(details_api, parameters)
        result = detail_resp.json()['result']
        reviewss = result['reviews']
        doc_name=result['name']
        doc_url = result['url']
        city_state = result['formatted_address']
        website = 'GOOGLE'
        specialty = 'Urologists'
        date_collected = 'June 15 2017'
        total_poss = '5'
        #gets multiple reviews of the physician(if any).
        for review in reviewss:
            rating = review['rating']
            revname = review['author_name']
            rev = review['text']
            date_review = review['relative_time_description']
            rev_url = review.get('author_url', '')

            writer.writerow([date_collected, doc_name, city_state, website, specialty, revname, date_review, rev_url, rating, total_poss, '', rev, doc_url])