scrape site with anti forgery token

2024/11/15 13:24:11

I'm trying to scrape data from website that uses anti forgery token what i tried to do is sending a get request then finding the key and use it to send a post request i was able to successfully scrape the key but the post request returns error page instead of the data page here is the code i used

note : using the key i scraped to send the request using browser console works fine

import requests
SeatingNo = '330814'headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36 Edg/104.0.1293.70'}res = requests.get("https://g12.emis.gov.eg/", headers=headers)
soup = BeautifulSoup(res.text, "html.parser")
__RequestVerificationToken = soup.find(type="hidden")['value']
print(__RequestVerificationToken)
data = {'SeatingNo': SeatingNo, '__RequestVerificationToken': __RequestVerificationToken}
res = requests.post("https://g12.emis.gov.eg/", data=data, headers=headers)
soup = BeautifulSoup(res.text, "html.parser")
print(soup)
Answer

i was able to solve it using cloudscrapper

import cloudscraper
scraper = cloudscraper.create_scraper()
SeatingNo = '330814'
res = scraper.get("https://g12.emis.gov.eg/")
soup = BeautifulSoup(res.text, "html.parser")
__RequestVerificationToken = soup.find(type="hidden")['value']
print(__RequestVerificationToken)
data = {'SeatingNo': SeatingNo, '__RequestVerificationToken': __RequestVerificationToken}
res = scraper.post("https://g12.emis.gov.eg/", data=data)
soup = BeautifulSoup(res.text, "html.parser")
print(soup)
https://en.xdnf.cn/q/119411.html

Related Q&A

Pandas merge and grouby

I have 2 pandas dataframes which looks like below. Data Frame 1: Section Chainage Frame R125R002 10.133 1 R125R002 10.138 2 R125R002 10.143 3 R125R002 10.148 4 R125R002 …

Find a pattern in the line of another file in python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 3…

AssertionError if running code in Python prompt but not if running as file

Why trying to explain here on stackoverflow what the Python command id() does and how can it be used to reveal how Python works under the hood I had run into following strange behavior I am struggling …

Remove values before and after special character

I have a dataframe, df, where I would like to remove the values that come before the underscore _ and after the underscore _ , essentially, keeping the middle. Also keeping the digits at the end and co…

Python selection sort

Question: The code is supposed to take a file (that contains one integer value per line), print the (unsorted) integer values, sort them, and then print the sorted values.Is there anything that doesnt…

Simple inheritance issue with Django templates

just getting started in Django, and I have some problems with the inheritances. It just seems that the loop for doesnt work when inheriting other template. Heres my code in base.html:<!DOCTYPE html&…

Replacing values in a list [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Azure Release Pipeline - Environment variables on python script

Lately Ive been requested to run a python script on my Azure Release Pipeline. This script needs some environment variables for being executed, as Ive seen that in the build pipeline, the task include …

Problem with python prepared stmt parameter passing

File C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\mysql\connector\cursor.py, line 1149, in execute elif len(self._prepared[parameters]) != len(params): TypeError: object of ty…

list of lists to list of tuples without loops or list comprehensions [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 6 years ago.Improve…