2017-02-25 29 views
0

隨後該鏈接,以urllib2的登錄: Login to website using urllib2 - Python 2.7解析與POST表單操作一個HTML BeautifulSoup有問題

我所試圖做的是創建存儲空間的虛擬地圖,要做到這一點,我需要登錄進入基於網絡的管理服務器。因此BS4的使用和urllib。

大部分請求都正常工作,除了手動登錄(通過網站)加載頁面時與使用urllib時HTML之間存在明顯區別。

這裏是它的外觀,當我在使用日誌記錄的urllib的方式的一個片段:

<div id="gridContainer" class='grid_12'></div> 

<form action="/Inventory/UnpendStorageSpaces" method="post"><input name="__RequestVerificationToken" type="hidden" value=">>>>>>BLOCKED VALUE>>>>=" /><input id="deviceKey" name="deviceKey" type="hidden" value="" /><input id="facilityItemKey" name="facilityItemKey" type="hidden" value="" /> 

這裏是它的外觀的一個片段,當我手動登錄(通過網站):

<div id="gridContainer" class="grid_12 gridContainer"> 
<div class="ui-jqgrid ui-widget ui-widget-content ui-corner-all" id="gbox_gridContainer_grid" dir="ltr" style="width: 940px;"> 
<div class="ui-widget-overlay jqgrid-overlay" id="lui_gridContainer_grid"></div><div class="loading ui-state-default ui-state-active" id="load_gridContainer_grid" style="display: none;">Loading ...</div> 
<div class="ui-jqgrid-view" id="gview_gridContainer_grid" style="width: 940px;"><div class="ui-jqgrid-titlebar ui-widget-header ui-corner-top ui-helper-clearfix" style="display: none;"><a role="link" href="javascript:void(0)" class="ui-jqgrid-titlebar-close HeaderButton" style="right: 0px;"><span class="ui-icon ui-icon-circle-triangle-n"></span></a><span class="ui-jqgrid-title"> 

正如你所看到的,兩者之間有明顯的區別,只是想看看最好的方法是什麼?

回答

1

由於網站使用javascript和JQuery來呈現網頁,因此通過urllib獲取頁面時不會發生這種情況,您會得到不同的結果。

查看the first answer以「用Python進行網頁抓取JavaScript頁面」獲得Python解決方案。其他選項包括使用Selenium或Phantomjs,但希望你不需要。

+0

對不起有一個愚蠢的時刻哈哈,想出瞭如何刮動態內容...基本上必須使用無頭webkit瀏覽器。 – user3866172