我写使用机械化模块,以打印出从列表中的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