BeautifulSoup在使用POST表单操作解析html时遇到问题

时间:2017-02-25 22:47:37

标签: python beautifulsoup urllib2 mechanize

按照此链接使用urllib2登录: Login to website using urllib2 - Python 2.7

我要做的是创建存储空间的虚拟地图,为此,我需要登录基于Web的管理服务器。因此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 个答案:

答案 0 :(得分:1)

您获得了不同的结果,因为该网站使用javascript和JQuery来呈现页面,这在通过urllib获取页面时不会发生。

使用Python&#34;查看the first answer到#34;抓取网页的JavaScript页面用于Python内解决方案。其他选择包括使用Selenium或Phantomjs,但希望你不必这么做。

相关问题