如何在python 3中获取此站点中的json数据?

问题描述 投票:1回答:1

我的工作基本上是:

- 进入这个网站“https://aplicacoes.mds.gov.br/sagirmps/estrutura_fisica/preenchimento_municipio_cras_new1.php

- 填写2个表格(例如AC - AcreBujari

- 单击生成的表的最后一列中的“Dados Detalhados”(详细数据)。 (当您单击“Dados Detalhados”时,它将生成第二个表,其中每行数据为1个月)。

- 访问第二个表生成的数据,在每行的最后一列中单击“VisualizarRelatório”。 <----那是我试图抓的数据。但它是一个动态的网站,我无法获取数据只是访问url2(当你点击'Visualizarrelacório'时,网站返回到初始网址,但我想要抓住的桌子)。这是代码:

import requests
from bs4 import BeautifulSoup  
import pandas as pd

url = 'http://aplicacoes.mds.gov.br/sagirmps/estrutura_fisica/preenchimento_municipio_cras_new1.php'
params ={
    'uf_ibge': '12',
    'nome_estado': 'AC - Acre'
    'p_ibge': '1200138'
    'nome_municipio': 'Bujari'    
}


r = requests.post(url, params = params, verify = False)
soup = BeautifulSoup(r.text, "lxml")
tables = pd.read_html(r.text)
unidades = tables[1]
print(unidades)


url2 = 'http://aplicacoes.mds.gov.br/sagirmps/estrutura_fisica/rel_preenchidos_cras.php?&p_id_cras=12001301971'
params2 ={
    'p_id_cras': '12001301971'
    'mes_referencia': '2019-02-01'
}
r2 = requests.post(url2, json = params2, verify = False)
soup2 = BeautifulSoup(r2.text, 'lxml')

soup2

请注意,url2是您单击“Dados Detalhados”时生成的URL,并且它具有'p_id_cras'作为第二个字典。

params2应该是用来刮掉我正在谈论的数据的词典。我在第二篇帖子请求中尝试了命令paramsdatajson,但它们都没有奏效。

python ajax web-scraping beautifulsoup python-requests
1个回答
1
投票

url2应该使用没有参数的GET。 然后你有一个页面与表格链接有href="javascript:" 还有onclick='enviadados(12001301971,"2019-02-01")' 所以你有下一个请求的参数。

最后一个请求使用带有参数POST和url的12001301971,2019-02-01

https://aplicacoes.mds.gov.br/sagirmps/estrutura_fisica/visualiza_preenchimento_cras.php'`

我的代码。我希望它能正常工作。

import requests
from bs4 import BeautifulSoup  
import pandas as pd

base = 'http://aplicacoes.mds.gov.br/sagirmps/estrutura_fisica/'

url = base + 'preenchimento_municipio_cras_new1.php'
#print('url:', url)
params ={
    'uf_ibge': '12',
    'nome_estado': 'AC - Acre',
    'p_ibge': '1200138',
    'nome_municipio': 'Bujari'    ,
}


r = requests.post(url, params=params, verify=False)
soup1 = BeautifulSoup(r.text, "lxml")
tables = pd.read_html(r.text)

#unidades = tables[1]
#print(unidades)

all_td1 = soup1.find('table', class_="panel-body").find_all('td')
#print('len(all_td1):', len(all_td1))
for td1 in all_td1:

    all_a1 = td1.find_all('a')[:1]
    #print('len(all_a1):', len(all_a1))
    for a1 in all_a1:

        url = base + a1['href']
        print('url:', url)

        r = requests.get(url, verify=False)
        soup2 = BeautifulSoup(r.text, "lxml")
        #print(soup.text)

        all_td2 = soup2.find('table', class_="panel-body").find_all('td')
        #print('len(all_td2):', len(all_td2))
        for td2 in all_td2:
            all_a2 = td2.find_all('a')
            #print('len(all_a2):', len(all_a2))
            for a2 in all_a2:
                print('onclick:', a2['onclick'])

                params = {
                    'p_id_cras': a2['onclick'][11:22], #'12001301971',
                    'mes_referencia': a2['onclick'][24:-2], #'2019-02-01',
                }

                print(params)

                url = 'https://aplicacoes.mds.gov.br/sagirmps/estrutura_fisica/visualiza_preenchimento_cras.php'
                r = requests.post(url, params=params, verify=False)
                soup = BeautifulSoup(r.text, "lxml")
                all_table = soup.find_all('table')
                print(all_table)
© www.soinside.com 2019 - 2024. All rights reserved.