无法使用Python从网站获取结果

时间:2017-07-13 18:52:45

标签: python-3.x http cookies

我有一个网页https://freddiemac.embs.com/FLoan/secure/login.php,我需要提供登录信息,然后在成功登录后,我将登陆不同的网页https://freddiemac.embs.com/FLoan/Data/download.php,我需要在其中勾选条款&条件复选框,然后单击继续,页面刷新以显示我需要下载的一堆zip文件。

我使用以下代码登录网站:

import urllib
import urllib.request
import http.cookiejar

payload = {'username': 'username', 'password': 'password','submit': 'Submit Credentials'}

cj = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
login_data = urllib.parse.urlencode(payload)
binary_data = login_data.encode('utf8')

opener.open('https://freddiemac.embs.com/FLoan/secure/login.php', binary_data)
resp = opener.open('https://freddiemac.embs.com/FLoan/Data/download.php')

print(resp.read())

我的结果低于结果,我觉得这只是https://freddiemac.embs.com/FLoan/secure/login.php页面的内容。

b'\n<!DOCTYPE html>\r\n<html>\r\n<head>\r\n<title>Freddie Mac Loan 
History</title>\r\n<link rel=\'stylesheet\' type=\'text/css\' 
href=\'/FLoan/app_corp.css\'>\r\n<link rel=\'stylesheet\' type=\'text/css\' 
href=\'/FLoan/empty.css\'>\r\n<link rel=\'stylesheet\' type=\'text/css\' 
href=\'/FLoan/empty2.css\'>\r\n</head>\r\n<body>\r\n<H2>Please log 
in</H2>\n\t<form name=\'loginform\' action=\'auth.php\' method=\'post\' 
class=\'form\'>\n\t<div class=\'row\'><div class=\'small-5 column\' 
>\n\t<label for="username" >Enter email address:</label>\n\t<input 
name=\'username\' type=\'email\' placeholder=\'name@company.com\'>
</p>\n\t</div></div>\n\t<div class=\'row\'><div class=\'small-5 column\' 
>\n\t<label for="password" >Enter password:</label>\n\t<input 
name=\'password\' type=\'password\' autocomplete=\'off\'>\n\t</div>
</div>\n\t<div class=\'row\'><div class=\'small-5 column\' >\n\t<input 
type=\'submit\' value=\'Submit Credentials\' class=\'fmSubmit\'>\n\t</div>
</div>\n\t</form>\n\t</p>\n\n</div></div></body></html>\n'

我按照线程How to use Python to login to a webpage and retrieve cookies for later usage?

下的步骤进行操作

感谢您的帮助!!

1 个答案:

答案 0 :(得分:1)

嗨Prashant Vikram Singh。我知道你刚才问过这个问题,但是当我自己寻找同一个问题的答案时,我遇到了你的问题。关键是要使用auth.php进行身份验证,然后传递您接受条款和条件。代码如下。希望这可以帮助您或任何遇到同样问题的人。

import requests

def get_freddie_links(payload1,payload2):
"""
This function takes in the data payloads for two http post to Freddie Mac Website, returns the list of issuance
disclosure files. 
"""
with requests.Session() as sess:
    response = sess.post('https://freddiemac.embs.com/FLoan/secure/auth.php',data=payload1)
    final_url = sess.post('https://freddiemac.embs.com/FLoan/Data/download2.php',data=payload2)
return final_url.text

singlefamily_payload = {'username' : <USERNAME>,'password' : <PASSWORD>}
payload2={'accept': 'Yes','acceptSubmit':'Continue','action':'acceptTandC'}
results = get_freddie_links(singlefamily_payload,payload2)