2011-05-12 23 views
1

我寫使用機械化模塊,以打印出從列表中的URL的HTML在python以下幾點:python mechanize - 可以登錄到網站,但不能搶html的URL列表?

import mechanize, fileinput 

urls = open('F:\Python\url_list.txt') 
content = [x.strip() for x in urls.readlines()] 
print content 

browser = mechanize.Browser() 
browser.open("https://login.asp") 
browser.select_form(nr=0) 
browser['desc'] = "xxxxx" 
browser['password'] = "xxxxx" 
response = browser.submit() 
logincheck = response.read() 

print logincheck 

# now logged into site, loop through the list of urls read in from the text file and print the html for each one: 

for s in content: 

    releasenote = browser.urlopen(s) 
    # error here, should be releasenote = browser.open(s) 

    print releasenote.geturl() 
    print releasenote.info() 
    print releasenote.read() 

我只是發現了以下錯誤然而,在Python Shell中:

Traceback (most recent call last): 
File "F:\Python\test.py", line 20, in <module> 
releasenote = browser.urlopen(s) 
File "C:\Python27\lib\site-packages\mechanize\_mechanize.py", line 628, in __getattr__ 
".select_form()?)" % (self.__class__, name)) 
AttributeError: mechanize._mechanize.Browser instance has no attribute urlopen (perhaps you forgot to .select_form()?) 

我在做什麼錯?乾杯!

+0

嘗試在for循環之前創建一個新瀏覽器,並在for循環中使用該新瀏覽器。我認爲選擇表單可能會以某種方式改變瀏覽器的狀態,從而爲您帶來這個錯誤。我不完全確定 – inspectorG4dget 2011-05-12 20:54:02

回答

2

這是mechanize.urlopen(s)

或如你所說browser.open(s)

+0

發現我的錯誤!: 'releasenote = browser.urlopen(s)'應該是: 'releasenote = browser.open(s)' 什麼是dumbass * rolleyes * – 2011-05-12 20:53:35

+0

@ 4rd2:請將此答案標記爲解決問題的人;謝謝。 – tzot 2012-03-10 10:09:09