我将如何提取如下所示的数据。例如,提取身体风格。我将在api中为一些条目执行此操作,而不仅仅是一个条目,所以我想我需要使用某种循环。非常感谢:D
>>> r3 = requests.get('www.websiteexample.com/api', params=header )
>>> print(r3)
"ListingId":4557112,"Title":"Audi A4 Luxary Ship for Sale 2005","Category":"0001-0268-0271-","StartPrice":17985.0000,"StartDate":"\\/Date(1457637570103)\\/","EndDate":"\\/Date(1458242370103)\\/","ListingLength":null,"IsBold":true,"AsAt":"\\/Date(1457680107428)\\/","CategoryPath":"\\/Trade-Me-Motors\\/Cars\\/Audi","Region":"Northland","Suburb":"Whangarei","NoteDate":"\\/Date(0)\\/","PriceDisplay":"$17,985","HasFreeShipping":true,"BodyStyle":"Sedan","Doors":0,"EngineSize":2000,"Make":"Audi","Model":"A4","Odometer":61988,"Year":2005,"Transmission":"Automatic","Fuel":"Petrol","NumberPlate":"ABC123","BestContactTime":"Evening","Cylinders":0,"Owners":0,"Vin":"WAUZZZ8EX5A457186","WofExpires":"\\/Date(0)\\/","RegistrationExpires":"\\/Date(0)\\/","StereoDescription":null,"ExteriorColour":"Dark Blue","ImportHistory":null,"IsDealer":false}],"FoundCategories":[]}'
上面的输出只是一个小提取物(否则太长)
答案 0 :(得分:1)
Reading the documentation,令人惊讶的是,第一个屏幕上requests
的第一个Google匹配的第一个示例告诉我们如何获取json正文:
>>> r3 = requests.get('www.websiteexample.com/api', params=header )
>>> struct = r3.json()
然后您可以打印所有顶级对象,例如
>>> for k, v in struct.items():
... print(k, v)
或FoundCategories
:
>>> struct['FoundCategories']
[]
答案 1 :(得分:0)
结构不是很好但是你可以使用pandas from pandas.io.json import json_normalize
中的json_normalize从json中提取数据。这不是来自请求。
import urllib, json
from pandas.io.json import json_normalize
url = "www.websiteexample.com/api"
response = urllib.urlopen(url)
data = json.loads(response.read())
data2 = json_normalize(data)