0

I am moving from VBA to Python and trying to migrate some code. I have so me good results so far but i get stuck every now and then.

This is the case.

from datetime import datetime
import requests
import re
from dateutil.relativedelta import relativedelta

date_from = datetime.now() - relativedelta(months=36)
date_to = datetime.now()


def login_tokyo(ism_imo):
    r_tokyo_entry = requests.get('https://apcis.tmou.org/public/')
    string_with_number = re.search("<span.*?>(.*?)</span>", r_tokyo_entry.text)
    numbers = re.findall('[0-9]+', str(string_with_number.group(1)))
    captcha = int(numbers[0]) + int(numbers[1])

    payload = {'captcha': captcha}

    r_tokyo_main = requests.post('https://apcis.tmou.org/public/?action=login',
                                 data=payload, cookies=r_tokyo_entry.cookies)

    payload1 = {'Param': '0', 'Value': ism_imo, 'imo': '', 'callsign': '', 'name': '', 'compimo': ism_imo,
                'compname': '', 'From': date_from.strftime('%d.%m.%Y'), 'Till': date_to.strftime('%d.%m.%Y'),
                'authority': '0', 'flag': '0', 'class': '0', 'ro': '0', 'type': "0", 'result': '0', 'insptype': '-1',
                'sort1': '0', 'sort2': 'DESC', 'sort3': '0', 'sort4': 'DESC'}

    r_tokyo_performance = requests.post('https://apcis.tmou.org/public/?action=getcompanies',
                                        data=payload1, cookies=r_tokyo_entry.cookies)
    print(r_tokyo_performance.text)


ism_imo = 3028090
login_tokyo(ism_imo)

When trying to print the print(r_tokyo_main.text) I get nothing when I was suposed to get some xml response.

Any suggestions? Edited to have the full code.

2 Answers 2

2

In This case you need to send back the cookies as well when sending post request.

r_tokyo_main = requests.post('https://apcis.tmou.org/public/?action=login', data=payload, cookies = r_tokyo_entry.cookies)

so, whatever cookies you received in get response, send them back

Sign up to request clarification or add additional context in comments.

5 Comments

The reponse body should be there. When doing via Chrome Inspec the response body contains the fields that will be used to do the intended search.
Modified the answer after inspecting the response in chrome . :)
That worked perfectly! Can you please briefly explain why are the cookies important in this case?
The cookie may be used for correlation purpose. As the server may need to know what was the captcha I sent to you and if it matches the captcha entered by you or not.
Thank you very much. I was trying to do the actual search after entering the website and can't go trough even using the cookies method. I have edited the post to show the full code.
1

The following should work. When you run the script, you will get this text SEARCH IN DATABASE from the target page:

import re
import requests

def login_tokyo(s):
    r = s.get('https://apcis.tmou.org/public/')
    str_number = re.findall("<span[^>]+(.*?)</span>", r.text)[0]
    numbers = re.findall('[0-9]+', str_number)
    captcha = int(numbers[0]) + int(numbers[1])
    payload = {'captcha': captcha}
    r = s.post('https://apcis.tmou.org/public/?action=login', data=payload)
    check_text = re.findall('<b>(.*?)</b>', r.text)[0]
    print(check_text)

if __name__ == '__main__':
    with requests.Session() as s:
        login_tokyo(s)

3 Comments

Hello @SIM. Thank you very much. I will investigate this one. I have to have more request later in the same session after login. Maybe this is the best way!
Hello @SIM I have been trying to learn this way but sadly I can't anchive the result. I have edited the post to complete the code. The last post request is not getting trough even when using your method. Any option?
Stop asking multidimensional questions in one go. You should create a new post describing your new issues as the one you found trouble dealing with in the first place has already been solved. Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.