2011-05-12 42 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