【问题标题】:Need help in using Mechanize module in Python script在 Python 脚本中使用 Mechanize 模块时需要帮助
【发布时间】:2014-06-30 07:43:01
【问题描述】:

我正在学习使用 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() 方法,如果我在第二个脚本中使用响应,则会出错。为什么会这样?

【问题讨论】:

    标签: python python-2.7 mechanize mechanize-python


    【解决方案1】:

    使用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

    【讨论】:

    • 非常感谢您的详细回复。欣赏!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多