【问题标题】:BeautifulSoup having problems parsing a html with POST form actionBeautifulSoup 在使用 POST 表单操作解析 html 时遇到问题
【发布时间】:2017-07-16 16:39:54
【问题描述】:

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

我要做的是创建存储空间的虚拟地图,为此我需要登录基于 Web 的管理服务器。因此是 BS4 用法和 urllib。

大多数请求都可以正常工作,除了通过手动登录(通过网站)加载页面时的 HTML 与使用 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">

如您所知,两者之间存在明显差异,只是想看看解决此问题的最佳方法是什么?

【问题讨论】:

    标签: python beautifulsoup urllib2 mechanize


    【解决方案1】:

    您会得到不同的结果,因为该站点使用 javascript 和 JQuery 来呈现页面,而通过 urllib 获取页面时不会发生这种情况。

    查看the first answer 到“使用 Python 抓取 JavaScript 页面”以获得 Python 内解决方案。其他选项包括使用 Selenium 或 Phantomjs,但希望您不必这样做。

    【讨论】:

    • 抱歉有一个愚蠢的时刻哈哈,想出了如何抓取动态内容...基本上必须使用无头 webkit 浏览器。
    猜你喜欢
    • 1970-01-01
    • 2021-05-27
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-24
    • 1970-01-01
    相关资源
    最近更新 更多