我正在学习使用 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() 方法,如果我在第二个脚本中使用响应,则会出现错误。为什么会发生?
最佳答案
使用 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
关于python - 在 Python 脚本中使用 Mechanize 模块需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24485174/