我正在尝试从以下链接下载数据:
NHTSA Website
如果我创建如下链接,它会在我的机器上下载CSV文件 http://webapi.nhtsa.gov/api/SafetyRatings/modelyear/2019/make/ACURA/model/RDX?format=csv
如何在Python中使用API读取不同车辆的此文件?
到目前为止,这是我的代码:
@RunWith(SpringJUnit4ClassRunner.class)
@WebMvcTest(value = SomeController.class, secure = false)
public class SomeControllerUTest {
@BeforeClass
public static void applySpringIntegration() {
new BeanFactoryMockUp();
}
@Before
public void setup() throws Exception {
mockMvc = MockMvcBuilders.standaloneSetup(someController).build();
}
MockMvc mockMvc;
@Injectable
private DepServiceImpl depService;
@Tested(availableDuringSetup = true,fullyInitialized = true)
private SomeController someController;
@Test
public void testSomeBehavior() throws Exception {
BeanFactory beanFactory = new DefaultListableBeanFactory();
DepService service = (DepServiceImpl) beanFactory.getBean("depService");
assertSame(mlService, service);
}
}
响应会自动将文件下载到我的下载文件夹吗?
答案 0 :(得分:1)
你可以试试这个
model = input("Enter model name: ")
year = input("year: ")
url = "http://webapi.nhtsa.gov/api/SafetyRatings/modelyear/"+year+"/make/ACURA/model/"+model+"?format=csv"
import urllib.request
with urllib.request.urlopen(url) as response:
html = response.read()
with open(model+year+".csv", "w") as f:
f.write(html)
您可以在网址中添加更多变量。然后阅读你可以使用pandas包。
import pandas as pd
vechiles = pd.read_csv("vichles.csv")