2011-05-05 23 views
1

我正在構建一個web刮板,並且需要獲取它實際出現在頁面上的html頁面源。但是,我只能獲得有限的html源代碼,其中不包含所需的信息。我認爲我要麼看到它的JavaScript加載否則可能我沒有得到完整的信息,因爲我沒有正確的認證?我的結果與Chrome瀏覽器中的「查看源代碼」相同,當我想要的是Chrome的「檢查元素」顯示的內容時。輸入航班信息和搜索後,我的測試是cimber.dk。獲取HTML源代碼,包括javascript和身份驗證的結果

我在python中編碼,並嘗試urllib2庫。然後我聽說硒對此很好,所以我也嘗試過。但是,這也讓我獲得了相同的有限頁面源。

這是我使用Firebug查看參數後用urllib2試過的。 (我打開cimber.dk後刪除了所有的cookies,因此我開始使用「clean slate」)

url = 'https://www.cimber.dk/booking/' 
values = {'ARRANGE_BY' : 'D',...} #one for each value 
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor()) 
#Using HTTPRedirectHandler instead of HTTPCookieProcessor gives the same. 
urllib2.install_opener(opener) 
request = urllib2.Request(url) 
opener.addheaders = [('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0) Gecko/20100101 Firefox/4.0')] 
request.add_header(....) # one for each header, also the cookie one 
p = urllib.urlencode(values) 
data = opener.open(request, p).read() 
# data is now the limited source, like Chrome View Source 

#I tried to add the following in some vain attempt to do a redirect. 
#The result is always "HTTP Error 400: Bad request" 

f = opener.open('https://wftc2.e-travel.com/plnext/cimber/Override.action') 
data = f.read() 
f.close() 

回答

2

大多數類似這樣的庫不支持JavaScript。

如果你想要javascript,你需要自動化一個現有的瀏覽器或瀏覽器引擎,或者得到一個非常龐大的大型庫,這本質上是一個先進的網絡爬蟲。

相關問題