简体   繁体   English

python请求库重定向新页面以发布请求

[英]python requests library redirect new page to post request

I am a newbie in python, and I have to retrieve the data of this page. 我是python的新手,我必须检索此页面的数据。 http://mims.com/india/drug/search?q=dextro but as this website requires user login so I went through the following code. http://mims.com/india/drug/search?q=dextro,但是由于该网站需要用户登录,因此我经历了以下代码。

import requests
from bs4 import BeautifulSoup as bs
POST_LOGIN_URL = "https://sso.mims.com/Account/Signin"
REQUEST_URL    = "https://mims.com/india/drug/search?q=DEXTRO%20PLUS"
payload = {
    "EmailAddress": "email address here",
    "Password"    : "password here"
}
with requests.Session() as session:
    post = session.post(POST_LOGIN_URL, data=payload)
    r = session.get(REQUEST_URL)
    with open('abc.html', 'wb')as f:
        f.write(r.content)

Instead of showing the desired result, it is redirecting me to a page and I am getting a "FORM" element which contains method="post" request and a button which autoclicks. 它没有将期望的结果显示出来,而是将我重定向到页面,并且我收到一个“ FORM”元素,其中包含method =“ post”请求和一个自动单击的按钮。 So I am unable to figure it out please someone help. 因此,我无法弄清楚,请有人帮助。

Solved it using selenium webdriver and wait after login or any other GET request. 使用Selenium Webdriver解决了该问题,并在登录或任何其他GET请求后等待
So that background request completes it's execution and return to the required page. 这样后台请求就完成了执行并返回到所需的页面。
Whenever you face redirections while Sending any GET request, add SLEEP(seconds) method it can solve many problems 发送任何GET请求时遇到重定向时,添加SLEEP(seconds)方法可以解决许多问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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