我已经完成了我的第一个 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"> </td>
<td id="rogueMajor" class="navAlarmNothing" align="right" width="23"> </td>
<td id="rogueMinor" class="navAlarmNothing" align="right" width="23"> </td>
</tr>
它给我' '(同样,反斜杠是我的,否则什么都不显示)它应该是'0'。
有谁知道这是为什么?以及如何修复?
最佳答案
javascript 是否可以负责打开 &\nbsp;变成0? Mechanize 不执行 javascript。
关于python mechanize.Browser 返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6258975/