我寫使用機械化模塊,以打印出從列表中的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()?)
我在做什麼錯?乾杯!
嘗試在for循環之前創建一個新瀏覽器,並在for循環中使用該新瀏覽器。我認爲選擇表單可能會以某種方式改變瀏覽器的狀態,從而爲您帶來這個錯誤。我不完全確定 – inspectorG4dget 2011-05-12 20:54:02