2012-08-15 343 views
0

我正在嘗試下載通過javascript請求的頁面的html,通常是通過單擊瀏覽器中的鏈接。我可以下載的第一頁,因爲它有一個通用網址:Python - 處理JavaScript網址?

http://www.locationary.com/stats/hotzone.jsp?hz=1 

但也有一起是數字(1〜10)的頁面底部的鏈接。所以,如果你點擊一個,它去,例如,第2頁:

http://www.locationary.com/stats/hotzone.jsp?ACTION_TOKEN=hotzone_jsp$JspView$NumericAction&inPageNumber=2 

當我把那個URL到我的程序,並嘗試下載HTML,它給了我一個不同的頁面上的HTML網站,我認爲這是主頁。

我怎樣才能得到這個網址的HTML使用JavaScript和沒有特定的網址?

謝謝。

P.S.我使用的是urllib/urllib2和cookielib。

另外,我剛剛發現了一些名爲PyQuery?我可以使用它嗎?我該怎麼做?

代碼:

import urllib 
import urllib2 
import cookielib 
import re 

URL = '' 

def load(url): 

    data = urllib.urlencode({"inUserName":"email", "inUserPass":"password"}) 
    jar = cookielib.FileCookieJar("cookies") 
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar)) 
    opener.addheaders.append(('User-agent', 'Mozilla/5.0 (Windows NT 6.1; rv:13.0) Gecko/20100101 Firefox/13.0.1')) 
    opener.addheaders.append(('Referer', 'http://www.locationary.com/')) 
    opener.addheaders.append(('Cookie','site_version=REGULAR')) 
    request = urllib2.Request("https://www.locationary.com/index.jsp?ACTION_TOKEN=tile_loginBar_jsp$JspView$LoginAction", data) 
    response = opener.open(request) 
    page = opener.open("https://www.locationary.com/index.jsp?ACTION_TOKEN=tile_loginBar_jsp$JspView$LoginAction").read() 

    h = response.info().headers 
    jsid = re.findall(r'Set-Cookie: (.*);', str(h[5])) 
    data = urllib.urlencode({"inUserName":"email", "inUserPass":"password"}) 
    jar = cookielib.FileCookieJar("cookies") 
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar)) 
    opener.addheaders.append(('User-agent', 'Mozilla/5.0 (Windows NT 6.1; rv:13.0) Gecko/20100101 Firefox/13.0.1')) 
    opener.addheaders.append(('Referer', 'http://www.locationary.com/')) 
    opener.addheaders.append(('Cookie','site_version=REGULAR; ' + str(jsid[0]))) 
    request = urllib2.Request("https://www.locationary.com/index.jsp?ACTION_TOKEN=tile_loginBar_jsp$JspView$LoginAction", data) 
    response = opener.open(request) 
    page = opener.open(url).read() 
    print page 

load(URL) 
+0

你能展示一些你正在使用的代碼嗎? – tijko 2012-08-15 01:41:57

回答