[英]retrieving a page that redirects to a login page within python
我在以編程方式從網站收集數據的過程很艱難。 我試圖利用該示例登錄服務器,但是由於我認為這是錯誤的登錄類型,因此無法正常工作。
當我嘗試下載數據以解析html時,我嘗試訪問的網站將重定向到登錄頁面。
這是URL:
https://mtred.com/rewards.html
這是代碼:
# build opener with HTTPCookieProcessor
o = urllib2.build_opener( urllib2.HTTPCookieProcessor() )
urllib2.install_opener( o )
# assuming the site expects 'user' and 'pass' as query params
p = urllib.urlencode( { 'UserLogin_username': 'mylogin', 'UserLogin_password': 'mypass' } )
# perform login with params
f = o.open( 'http://www.mtred.com/user/login.html', p )
data = f.read()
f.close()
# second request should automatically pass back any
# cookies received during login... thanks to the HTTPCookieProcessor
f = o.open( 'https://www.mtred.com/rewards.html',p )
data = f.read()
print data
當我嘗試打開獎勵時,它將再次將我踢到登錄頁面。 我正在嘗試通過獎勵自動執行一些統計信息,因為該信息無法通過公共API獲得
出現的一個問題是您要傳遞登錄信息的表單參數的id
值,而不是name
參數。 例如,在用戶名表單字段中,您指定UserLogin_username
,但是服務器期望的該字段的名稱為"UserLogin[username]"
<label for="UserLogin_username" class="required">
username or email <span class="required">*</span></label>
<input name="UserLogin[username]" id="UserLogin_username" type="text" /> </div>
<div class="row">
<label for="UserLogin_password" class="required">password <span class="required">*</span></label>
<input name="UserLogin[password]" id="UserLogin_password" type="password" /> </div>
由於服務器沒有取回它知道的參數,因此您所看到的行為並不意外。 (不是說這里沒有其他問題;沒看過。)
您必須在您的發布數據中包含html表單中包含的名為“ YII_CSRF_TOKEN”的值。 或使用“ ClientForm ”庫
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.