您可能希望并行发送请求.Python提供了multiprocessing
适合这样的任务的模块.
示例代码:
from multiprocessing import Pool def get_data(i): r1 = requests.get('https://maps.googleapis.com/maps/api/place/textsearch/json?query='+ i +'&key=MY_KEY') a = r1.json() pid = a['results'][0]['place_id'] r2 = requests.get('https://maps.googleapis.com/maps/api/place/details/json?placeid='+pid+'&key=MY_KEY') b = r2.json() phone = b['result']['formatted_phone_number'] name = b['result']['name'] website = b['result']['website'] return ' '.join((phone, name, website)) if __name__ == '__main__': terms = input("input places separated by comma").split(",") with Pool(5) as p: print(p.map(get_data, terms))