Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error with requests
#1
Trying to login using requests but I'm getting html returned that appears to be an error page.

Python:
import requests

with requests.Session() as c:
    url = "http://10.10.....html"
    user_name = input("Enter your username: ")
    password = input("Enter your password: ")

    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'}

    c.get(url, headers = headers)
    login_data = dict(txt_inptUserId = user_name, txt_password = password, headers = headers)
    c.post(url, data = login_data)

    page = c.get(url)
    print(page.content)
HTML-Error?:
Quote:C:\Users\user\Desktop\requests>test.py
Enter your username number: xyz
Enter your password: xyz
b'<html>\r\n <head>\r\n <title>App</title>\r\n </head>\r\n <frameset na
me="jacadaframeset" frameborder="0" framespacing="0" rows="100%,*">\r\n <fram
e name="jacadaframe" frameborder="0" marginheight="0" marginwidth="0" src="/Xhtm
l?JacadaApplicationName=App"/>\r\n </frameset>\r\n <noframes>\r\n <p>You
r browser doesn\'t support frames!<p>\r\n window.location.href="/Xhtml?Jacada
ApplicationName=App";\r\n </noframes>\r\n</html>\r\n'
Reply
#2
They problem is likely in your log in method,then the return is correct.
It's not possible to help without seeing the page,there is a lot of different method log-in method that web-pages use.
You have to inspect web-page to see what's happens when log in,eg use Chrome/FireFox developer tool.
Have posted this before,example how to log in into this forum see that i need to find params used.
I use Session() so it's persist against requests and cookies.
import requests
from bs4 import BeautifulSoup

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
}

params = {
    "username": "your_username",
    "password": "xxxxxxx",
    "remember": "yes",
    "submit": "Login",
    "action": "do_login",
}

with requests.Session() as s:
    s.post('https://python-forum.io/member.php?action=login', headers=headers, params=params)
    # logged in! session cookies saved for future requests
    response = s.get('https://python-forum.io/index.php')
    # cookies sent automatically!
    soup = BeautifulSoup(response.content, 'lxml')
    welcome = soup.find('span', class_="welcome").text
    print(welcome)
Output:
Welcome back, snippsat. You last visited: Today, 05:44 PM Log Out
Reply
#3
Here's a rundown of the tags/elements:
<input id="inptUserId" maxlength="8"
	name="txt_inptUserId" onkeyup="autoSkipNN(event.which,this);" onfocus="saveFocusedControl(&quot;inptUserId&quot;);" style="background-color:Window;font-size:11px;height:19px;width:67px;line-height:11px;left:275px;padding-bottom:0;font-family:Arial;border-width:2px;position:absolute;border-style:inset;top:101px;color:000000;padding-top:0;" tabindex="26"
	title="" type="text" value="">
<input id="password" maxlength="8"
	name="txt_password" onkeyup="autoSkipNN(event.which,this);" onfocus="saveFocusedControl(&quot;password&quot;);" style="background-color:Window;font-size:11px;height:19px;width:67px;line-height:11px;left:445px;padding-bottom:0;font-family:Arial;border-width:2px;position:absolute;border-style:inset;top:101px;color:000000;padding-top:0;" tabindex="28"
	title="" type="password" value="">
Then I need to click this button after logging in:

<input alt="Enter" id="btnOK"
	name="btn_btnOK" onfocus="saveFocusedControl(&quot;btnOK&quot;);" style="font-family: Arial; font-size: 12px; font-weight: Bold; position: absolute; left: 19px; top: 292px; width: 49px; height: 23px; color: #ffffcc; background-color: transparent; border-width: 0px; text-align:left;padding-left:0px;padding-right: -5px;cursor: pointer; padding-left:0px;padding-right:0px;" tabindex="-1"
	title="" type="submit" value="Enter">
(May-07-2019, 04:34 PM)snippsat Wrote: I use Session() so it's persist against requests and cookies.
What is Session()? Is that a function/method of requests or another module itself? Thanks.

Also, does this help?
Form Data
Output:
Applname: App Formname: SIGNON sessionId: 180... sequenceNumber: 2 focusedControl: NewPassword keepAliveURL: /KeepAlive? keepAliveInterval: 60000 clientDebugLevel: 0 defaultbtn_btnOK: activate default default_btnOK: 1 txt_txtActiveHelpHtml: txt_txtPrevFocus: txt_txtRecordFlag: txt_txtMacros: txt_txtFieldValue: txt_txtMugShots: txt_txtSavedHelpValue: txt_txtFavorites: txt_txtTableLinkStyle: -1 txt_txtKeyBoardSupport: txt_inptUserId: myUserName txt_password: abc123 txt_NewPassword:
Reply
#4
I fixed code tags in your post,look at BBCode
rudolphyaber Wrote:What is Session()? Is that a function/method of requests or another module itself? Thanks.
Session Objects when you use a browser the states eg cookies is kept.
Doing it outside of browser need to keep state persist,when eg doing request(get, post).
You are using Session in your post so that's okay.

rudolphyaber Wrote:Also, does this help?
Form Data
Is this form data of html on site?
You have to look in Network tab of Chrome/FireFox developer tool,when you try to log in browser.
Then Form Data that get send can be found.
Reply
#5
(May-08-2019, 08:48 AM)snippsat Wrote: Is this form data of html on site?
You have to look in Network tab of Chrome/FireFox developer tool,when you try to log in browser.
Then Form Data that get send can be found.

Yes it got this from under the Network tab of Chrome's developer tools.
Reply
#6
Not sure without looking is all of that is send as form data.
Can try this, if fail try to send all that you posted.
params = {
txt_txtActiveHelpHtml: '',
txt_txtPrevFocus:  '',
txt_txtRecordFlag: '',
txt_txtMacros: '',
txt_txtFieldValue: '',
txt_txtMugShots: '',
txt_txtSavedHelpValue: '',
txt_txtFavorites:  '',
txt_txtTableLinkStyle: -1,
txt_txtKeyBoardSupport: '',
txt_inptUserId: myUserName ,
txt_password: abc123 ,
txt_NewPassword:  '',
}
Reply
#7
(May-11-2019, 08:36 AM)snippsat Wrote: Not sure without looking is all of that is send as form data.
Can try this, if fail try to send all that you posted.
All that the user enters into the form in the browser is the username and password. I also have two screenshots but the image button on here only lets me use a url, I don't see a way to upload the images.

I added this code:
    params = {  txt_txtActiveHelpHtml: '',
                txt_txtPrevFocus:  '',
                txt_txtRecordFlag: '',
                txt_txtMacros: '',
                txt_txtFieldValue: '',
                txt_txtMugShots: '',
                txt_txtSavedHelpValue: '',
                txt_txtFavorites:  '',
                txt_txtTableLinkStyle: -1,
                txt_txtKeyBoardSupport: '',
                txt_inptUserId: {user_name},
                txt_password: {password},
                txt_NewPassword:  '',
and this is what I got:
params = { txt_txtActiveHelpHtml: '',
NameError: name 'txt_txtActiveHelpHtml' is not defined


It actually give this error for all of them. But these are the variable that chrome shows for form data for logging in.
Reply
#8
All should be string,so like this.
params = {
"txt_txtActiveHelpHtml": '',
"txt_txtPrevFocus":  '',
"txt_txtRecordFlag": '',
"txt_txtMacros": '',
"txt_txtFieldValue": '',
"txt_txtMugShots": '',
"txt_txtSavedHelpValue": '',
"txt_txtFavorites":  '',
"txt_txtTableLinkStyle": -1,
"txt_txtKeyBoardSupport": '',
"txt_inptUserId": 'myUserName',
"txt_password": 'abc123',
"txt_NewPassword":  '',
}
Reply
#9
(May-14-2019, 07:53 PM)snippsat Wrote: All should be string,so like this.
The returned html has the correct title for the page between the <title> tags and so does the ApplicationName but still don't think I'm getting a good return, this is what I'm getting:

b'<html>\r\n
<head>\r\n
<title>APP</title>\r\n
</head>\r\n
<frameset name="jacadaframeset" frameborder="0" framespacing="0" rows="100%,*">\r\n
<frame name="jacadaframe" frameborder="0" marginheight="0" marginwidth="0" src="/Xhtml?JacadaApplicationName=APP"/>\r\n
</frameset>\r\n <noframes>\r\n
<p>Your browser doesn\'t support frames!<p>\r\n window.location.href="/Xhtml?JacadaApplicationName=APP";\r\n
</noframes>\r\n</html>\r\n'
Reply
#10
(May-14-2019, 07:53 PM)snippsat Wrote: All should be string,so like this.
And I did make that change.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  POST requests - different requests return the same response Default_001 3 1,901 Mar-10-2022, 11:26 PM
Last Post: Default_001
  Error posting with requests julio2000 6 2,774 Mar-28-2020, 09:43 PM
Last Post: julio2000
  Error in requests.post debanilroy 3 5,364 Sep-18-2018, 06:15 PM
Last Post: snippsat
  An Error in Requests Module pratheep 3 11,870 Feb-06-2018, 05:17 PM
Last Post: pratheep

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020