通过使用来自websi的查询获取数据

2024-10-01 19:27:38 发布

您现在位置:Python中文网/ 问答频道 /正文

使用Python或R,如何将以下网站的数据下载为dataframe或类似格式?我假设这不是web抓取,而是通过查询请求获取数据。你知道吗

https://www.michigantrafficcrashfacts.org/querytool/lists/0#q1;0;2016;;


Tags: 数据httpsorgwebdataframe网站www格式
1条回答
网友
1楼 · 发布于 2024-10-01 19:27:38

你不需要做网页刮板,我观察到所有的请求,该网站,然后我解码这些请求与我的工具….,请参阅:

1)https://www.michigantrafficcrashfacts.org/qjson?q=1;0;2016;;&v=list&p=0,0:0,77|0|90|0,asc:1,asc| | 90记录为json

2)https://www.michigantrafficcrashfacts.org/qjson?q=1;0;2016;;&v=list&p=0,0:0,77|0|2000|0,asc:1,asc ||2000条记录为json

import json, requests

your_records = 3000  # change this record with that number you want ( that website said the max value is 312172 )

URL = "https://www.michigantrafficcrashfacts.org/qjson?q=1;0;2016;;&v=list&p=0,0:0,77|0|{0}|0,asc:1,asc".format(your_records)

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}

get_data = requests.get(URL, headers=headers)

raw_data = str(get_data.content, encoding="utf-8")

dict_data = json.loads(raw_data)

for items, values in dict_data.items():
    print(items, values)

注意:请不要在非法或黑客案件中使用。

相关问题 更多 >

    热门问题