2010-12-11 120 views
7

我見過這個其他問題:How to use Python to login to a webpage and retrieve cookies for later usage?如何使用python登錄網站?

但是,直接修改該答案並不適用於我,所以我想知道如何才能實現我的目標。

爲了讓背景下,我試圖登錄到https://mog.com/hp/sign_in,然後從以下頁面提取我的播放列表的名稱:http://mog.com/my_mog/playlists

我認爲這應該是有人誰知道他們在做什麼,很簡單。登錄網站並訪問受密碼保護的頁面的一些基本代碼會很好,如果你能用一兩句話來解釋代碼中的每一行代碼在做什麼,那將會更好,因此我可以更好地理解代碼在做什麼。

+0

你改變了什麼?你怎麼知道它不適合你? – 2010-12-11 01:32:13

+0

我更改了網站和登錄信息。我知道這是行不通的,因爲我打印出了頁面上的鏈接,而且鏈接不一樣。它從註冊頁面打印出鏈接。 – jonderry 2010-12-11 01:41:09

+0

也許我沒有正確修改這一行:'login_data = urllib.urlencode({'username':username,'j_password':password})''但我不知道如何弄清楚需要做什麼替換。 – jonderry 2010-12-11 01:46:25

回答

12

嘗試用mechanize

import mechanize 
br=mechanize.Browser() 
br.open('https://mog.com/hp/sign_in') 
br.select_form(nr=0) 
br['user[login]']= your_login 
br['user[password]']= your_password 
br.submit() 
br.retrieve('http://mog.com/my_mog/playlists','playlist.html') 

編輯:
讓你的鏈接,你可以補充一點:

for link in br.links(): 
    print link.url, link.text 

,或者從playlist.html開始,你可以使用Beautifulsoup和正則表達式:

from BeautifulSoup import BeautifulSoup 
import re 
soup = BeautifulSoup(file('playlist.html').read()) 
for link in soup.findAll('a', attrs={'href': re.compile("your matching re")}): 
    print link.get('href') 
+1

我得到這個工作,並將HTML拉到文件,如書面。如果我想檢索一些與mog.com/my_mog/playlists中的模式相匹配的鏈接,那麼該命令是什麼?我無法找到清晰,易於搜索的機械化文檔。 – jonderry 2010-12-11 02:54:15

+3

@jonderry我不認爲有一個命令,你可能想使用正則表達式匹配模式 – Asterisk 2010-12-11 05:08:50