简体   繁体   中英

Python requests response getting wrong url

I'm trying to request data from a url, but constantly get 404 error

session = requests.Session()
GET = session.get("http://www.inmet.gov.br/sonabra/pg_dspDadosCodigo_sim.php?QTgwNA==", timeout=1)
GET.raise_for_status()

soup = bs4.BeautifulSoup(GET.text, 'html.parser')
imgNumber  = soup.img.get("src").split("imgNum=", maxsplit=1)[1]
decodeNumber = str(base64.b64decode(imgNumber), 'utf-8')

request = {"dtaini": self.start, "aleaValue": imgNumber,
                  "aleaNum": decodeNumber, "dtafim": self.end}

POST = session.post(stationURL, data=request)
POST.raise_for_status()

logging result:

urllib3.connectionpool: DEBUG: Starting new HTTP connection (1): www.inmet.gov.br
urllib3.connectionpool: DEBUG: http://www.inmet.gov.br:80 "GET /sonabra/pg_dspDadosCodigo_sim.php?QTgwNA==  HTTP/1.1" 200 690
urllib3.connectionpool: DEBUG: /sonabra/pg_dspDadosCodigo_sim.php?QTgwNA==  HTTP/1.1" 302 498
urllib3.connectionpool: DEBUG: http://www.inmet.gov.br:80 "GET /sonabra/log2/index.php HTTP/1.1" 404 302

I'm not sure what happened as it used to work some months ago and the request do work at browser. I appreciate any help or advise.

它与python无关,我什至只是通过使用Web浏览器就得到了404,尝试使用其他URL或与服务器所有者联系(如果您认为不应这样做)。

For some reason I was getting confusing debugging showing me old forms in both Chrome and Firefox, the reason for it to not work is because the form data interface changed from:

request = {"dtaini": self.start, "aleaValue": imgNumber,
           "aleaNum": decodeNumber, "dtafim": self.end}

to:

request = {"aleaValue": aleaValue, "xaleaValue": xaleaValue,
           "aleaNum": aleaNum, "xID": xID,
           "dtaini": self.start, "dtafim": self.end}

My suggestion to someone with similar problem is to take a rest, look things better next morning, with coffee...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM