python - 在 Python 脚本中使用 Mechanize 模块需要帮助

标签 python python-2.7 mechanize mechanize-python

我正在学习使用 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/

相关文章:

Python + Mechanize 不适用于 Delicious

python - 在 Amazon Sagemaker Jupyter 笔记本中导入自定义模块

python - 在 wxpython TextCtrl.WriteText() 之后防止换行

python - sqlalchemy mixin、外键和关系

python - 如何根据 Pandas 中的行值创建新列

python mechanize - 从触发文件下载的 aspnetForm submitControl 检索文件

Python-使用颜色和字体写入文件

python - 使用 python 中的线程从无限循环中读取结果

python-2.7 - Amazon EMR Pyspark : rdd. unique.count() 失败

ruby - 使用 Mechanize 和 Ruby 解析帧中的数据