迭代HTTP请求标头

时间:2018-08-05 14:13:40

标签: python-3.x python-requests

我一直在使用一些代码来迭代来自Webmap的http请求以刮取整个城市的地块信息,我是request python模块的新手,并且我得到了一些帮助以获取单个代码请求,我想要的是按包裹块和数字进行迭代的请求,但是我得到了400的响应,我无法弄清楚问题出在哪里,对于每个请求我都需要一个新的cookie吗?有没有更好的方法这样做吗?,我想念的是什么?,

谢谢

link to page->“ BUSCAR ROL”“ comuna = antofagasta,manzana =(示例)234,predio =(示例)1”

import requests


url = "https://www4.sii.cl/mapasui/services/data/mapasFacadeService/getPredioNacional"

lista = []

#small range for testing
for q in range(100, 102):
    for w in range(0,5):
        pl = "{\"metaData\":{\"namespace\":\"cl.sii.sdi.lob.bbrr.mapas.data.api.interfaces.MapasFacadeService/getPredioNacional\",\"conversationId\":\"UNAUTHENTICATED-CALL185.230.124.50\",\"transactionId\":\"83080ee0-f0e2-72c6-bbb8-48a652fda2b6\"},\"data\":{\"predio\":{\"comuna\":2201,\"manzana\":%s,\"predio\":%s,\"}}}"%(q, str(w))
        lista.append(pl)

print(lista)

print(len(lista))

headers = {
    'cookie': "JSESSIONID=53EADCAA4C791671CBF9DF43ABEA6B1D.mrt01; AAGSID=53EADCAA4C791671CBF9DF43ABEA6B1D.mrt01",
    'pragma': "no-cache",
    'origin': "https://www4.sii.cl",
    'accept-encoding': "gzip, deflate, br",
    'accept-language': "en-US,en;q=0.8",
    'user-agent': "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36",
    'content-type': "application/json",
    'accept': "application/json, text/plain, */*",
    'cache-control': "no-cache",
    'referer': "https://www4.sii.cl/mapasui/internet/",
    'connection': "keep-alive"
    }

response = []

for m in lista:
    req = requests.request("POST", url, data=m, headers=headers)
    response.append(req.json)



print(response)

编辑:

我找到了一种方法:

import requests

url = "https://www4.sii.cl/mapasui/services/data/mapasFacadeService/getPredioNacional"

lista = []

for q in range(100, 500):
    for w in range(0,25):
        pl = '{"metaData":{"namespace":"cl.sii.sdi.lob.bbrr.mapas.data.api.interfaces.MapasFacadeService/getPredioNacional","conversationId":"UNAUTHENTICATED-CALL194.99.104.27","transactionId":"72030a64-b3e8-dc82-14cc-733564c821b7"},"data":{"predio":{"comuna":"2201","manzana":"%s","predio":"%s"},"servicios":[{"comuna":2201,"layer":"sii:BR_CART_ANTOFAGASTA_WMS","style":"PREDIOS_WMS_V0"},{"comuna":2201,"layer":"sii:BR_CART_AH_MUESTRAS","style":"AH_MUESTRA"}]}}'%(q, w)
        lista.append(pl)


#pl = '{"metaData":{"namespace":"cl.sii.sdi.lob.bbrr.mapas.data.api.interfaces.MapasFacadeService/getPredioNacional","conversationId":"UNAUTHENTICATED-CALL194.99.104.27","transactionId":"72030a64-b3e8-dc82-14cc-733564c821b7"},"data":{"predio":{"comuna":"2201","manzana":"%s","predio":"%s"},"servicios":[{"comuna":2201,"layer":"sii:BR_CART_ANTOFAGASTA_WMS","style":"PREDIOS_WMS_V0"},{"comuna":2201,"layer":"sii:BR_CART_AH_MUESTRAS","style":"AH_MUESTRA"}]}}'%(234, 2)

print(lista)

print(len(lista))

headers = {
    'host':"www4.sii.cl",
    'cookie': "JSESSIONID=8A6FDE8962A11231DB70F9A259B2A669.mos05; AAGSID=8A6FDE8962A11231DB70F9A259B2A669.mos05",
    'pragma': "no-cache",
    'origin': "https://www4.sii.cl",
    'accept-encoding': "gzip, deflate, br",
    'accept-language': "en-US,en;q=0.8",
    'user-agent': "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0",
    'content-type': "application/json",
    'accept': "application/json, text/plain, */*",
    'cache-control': "no-cache",
    'referer': "https://www4.sii.cl/mapasui/internet/",
    'connection': "keep-alive",
    'content-length':"448"
    }

response = []

for m in lista:
    req = requests.request("POST", url, data=m, headers=headers)
    response.append(req.text)


with open("predios.txt", "w") as text_file:
    text_file.write(str(response))    

0 个答案:

没有答案