簡體   English   中英

在Python腳本中使用Mechanize模塊時需要幫助

[英]Need help in using Mechanize module in Python script

我正在學習使用機械化來解析數據,但腳本中存在問題:

腳本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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM