我正在学习使用mechanize解析数据,但我在脚本中遇到了问题:
脚本1
import mechanize
myBrowser = mechanize.Browser()
myBrowser.open("http://realpython.com/practice/aphrodite.html")
print myBrowser.response().get_data()
脚本2
import mechanize
myBrowser = mechanize.Browser()
htmlPage = myBrowser.open("http://realpython.com/practice/aphrodite.html")
print htmlPage.get_data()
现在两个脚本的区别很小。首先,myBrowser.open()未分配给变量,而在第二个中,它被分配给名为htmlPage的变量。现在的问题是,根据我的知识,你需要使用响应方法和响应方法,如get_data来获取网页的数据。但是在我的第二个脚本中,我没有使用响应方法并直接使用get_dat()方法,如果我在第二个脚本中使用响应,则会出错。为什么会这样?
答案 0 :(得分:0)
使用htmlPage = myBrowser.open("http://realpython.com/practice/aphrodite.html")
将变量分配给mechanize.Browser().open()
,这样您只能访问mechanize.Browser().open()
方法和属性。
在第二个示例中,您使用的是myBrowser = mechanize.Browser()
,因此您可以访问所有mechanize.Browser()
方法和属性。
mechanize.Browser().open().resonse
mechanize.Browser().response
方法