python mechanize.Browser 返回

标签 python mechanize web-scraping

我已经完成了我的第一个 python 脚本,也是我的第一个 Mechanize 浏览器,非常好。但...

它返回一些字段作为 'empty string' ,在这种情况下是: "&\nbsp;"(反斜杠是我的,否则什么都不会出现。)

有没有人在 mechanize.Browser 之前经历过这种情况?

这是我的代码:

#!/usr/bin/env python

import mechanize
import cookielib
# Browser
br = mechanize.Browser()
# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
# Browser options
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
# Follows refresh 0 but not hangs on refresh > 0
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
# Want debugging messages?
#br.set_debug_http(True)
#br.set_debug_redirects(True)
#br.set_debug_responses(True)
# User-Agent (this is cheating, ok?)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
r = br.open('https://192.168.2.141/webacs/welcomeAction.do')
br.select_form(nr=0)
br.form['username']='root'
br.form['password']='bla bla bla'
submit = br.submit()
import time
# just in case is about rendering time, which is not. 
time.sleep(10)
wcs_web = br.response().read()
file = open("wcs_web.html",'w')
file.write(wcs_web)
file.close()

在文件 wcs_web.html 中,我发现了这个:
  <tr>
    <th class="navAlarm" width="92">Malicious AP</th>
    <td id="rogueCritical" class="navAlarmNothing" align="right" width="23">&nbsp;</td>
    <td id="rogueMajor" class="navAlarmNothing" align="right" width="23">&nbsp;</td>
    <td id="rogueMinor" class="navAlarmNothing" align="right" width="23">&nbsp;</td>
  </tr>

它给我' '(同样,反斜杠是我的,否则什么都不显示)它应该是'0'。

有谁知道这是为什么?以及如何修复?

最佳答案

javascript 是否可以负责打开 &\nbsp;变成0? Mechanize 不执行 javascript。

关于python mechanize.Browser 返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6258975/

相关文章:

python - 杂乱的扭曲连接在不干净的时尚中消失了。没有代理。已经尝试过标题

python - 如何断开与 elasticsearch-py 客户端/连接池的连接

python - Pylint 不能在 OS X 上使用 Emacs GUI;从命令行工作

python - 在 mechanize 中禁用 ssl 证书验证

python - 关注 Mechanize 的链接

c# - HTML Agility Pack 找不到 xPath

python - "Browser Not Supported"使用BeautifulSoup进行网页抓取时出错

python - Django Rest 从 FileField url 获取文件

python - 为什么 sys.path 是一个列表?

ruby-on-rails - 如何将 rake 任务的输出显示到浏览器?