获取非默认时间段需要请求三次
import requests
import time
import re
import urllib3
urllib3.disable_warnings()
#params2内修改需要获取数据的时间段
headers = {
'Host':'data.stats.gov.cn',
"Referer": "https://data.stats.gov.cn/easyquery.htm?cn=A01",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36",
}
url = "http://data.stats.gov.cn/easyquery.htm"
params0={
'id': 'A06',#房地产
'dbcode': 'hgyd',
'wdcode': 'zb',
'm': 'getTree'
}
params1={
'm': 'QueryData',
'dbcode': 'hgyd',
'rowcode': 'zb',
'colcode': 'sj',
'wds': '[]',
'dfwds': '[{"wdcode":"zb","valuecode":"A0609"}]',
'k1': round(time.time()*1000),
'h':1
}
params2={
'm': 'QueryData',
'dbcode': 'hgyd',
'rowcode': 'zb',
'colcode': 'sj',
'wds': '[]',
'dfwds': '[{"wdcode":"sj","valuecode":"2000-2022"}]',#这里修改需要获取数据的时间段
'k1': round(time.time()*1000),
}
s = requests.session()
response = s.post(url, headers=headers,params=params0, verify=False)
def get_sales_volume():#销售额数据
response = s.get(url, headers=headers,params=params1, verify=False)
response = s.get(url, headers=headers,params=params2, verify=False)
response.encoding='utf-