R
R
Ruslan Kusmanov2018-09-12 09:40:27
Python
Ruslan Kusmanov, 2018-09-12 09:40:27

Request python post passing JS script data?

Good afternoon, please help I
just started to deal with parsing in python.
I'm trying to get data from the site https://kase.kz/ru/trades/view/
The problem is that, to open this link, you need to select check boxes with the necessary information at the link https://kase.kz/ru/trades/
I wrote this code, but I can't get the page

import requests
from bs4 import BeautifulSoup
payload = {'csrfmiddlewaretoken':'6H4ZKPzNbRpguqRqcu2LKDUeT06CwtwP7i9yZye2ubJqrCOE0jLsuj0zYfMKtvX4','market': 'FOREX','fields': '3' , 'instruments': '"USDKZT_TOD"'}
page = requests.post('https://kase.kz/ru/trades/view/', data = payload)
print(page.status_code)

result 403

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xoo, 2018-09-12
@Qsm21

Maybe like this

import requests
import re

url = 'https://kase.kz/ru/trades/'

r = requests.get(url)

cookie_header = r.headers['Set-Cookie']
cookie = re.search('csrftoken=([\s\S]+?);', cookie_header).group(1)

print cookie

cookies = {
    'csrftoken': cookie,
    'TRADE_USER_SETTINGS_FOREX': '"{\"fields\": [3]\054 \"instruments\": [\"USDKZT_TOD\"]}"',
}
data = {
    'csrfmiddlewaretoken': cookie,
    'market': 'FOREX',
    'instrument': 'USDKZT_TOD',
    'field': '3',
}
r = requests.post('https://kase.kz/ru/trades/view/', cookies=cookies, data=data)
print r.status_code
print r.text

Answer 200, everything is fine, the document is given

R
Ruslan Kusmanov, 2018-09-12
@Qsm21

Rewrote the code to

import requests
from bs4 import BeautifulSoup
headers = {
    'Upgrade-Insecure-Requests': '1',
    'Content-Type': 'application/x-www-form-urlencoded'
}
cookies = {
    'csrftoken': 'pjxuo6wCuMcSyqKN7U5P3GafthpzBJs4qUC3DPbRN6w2vCH1VJOwNmgAyw5HyLTj',
    'TRADE_USER_SETTINGS_FOREX': '"{\"fields\": [3]\054 \"instruments\": [\"USDKZT_TOD\"]}"',
}
data = {
    'csrfmiddlewaretoken': 'ICIZdcQ4t80pwinw6DjCrTeJA6b70093JdNysVvjMskztukKUs2jbzk4FlRfX2Ai',
    'market': 'FOREX',
    'instrument': 'USDKZT_TOD',
    'field': '3',
}
r = requests.post('https://kase.kz/ru/trades/view/', headers=headers, cookies=cookies, data=data)
print(r.status_code)

Now the question is how to get them, how to get them, they are issued by the server

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question