2
votes

I am trying to automate a process on a website using the python mechanize module.

With the following code, I am refreshing the site until I find a link that I want to follow:

            for link in br.links():            
               valuable = filter(lambda tpl: tpl[0]=='href', link.attrs)
               if len(valuable) == 0:
                   continue
               if 'book.php' in valuable[0][1]:
                   print link
                   req = br.follow_link(link)
                   file_handler = open("level2.html", "w")
                   file_handler.write(br.response().read())
                   file_handler.close()
                   not_done = False
                   break

As soon as I find the link containing "book.php", the application crashes and I get the following error:

File "/home/max/Dokumente/dev/main.py", line 43, in run req = br.follow_link(link)

File "build/bdist.linux-x86_64/egg/mechanize/_mechanize.py", line 569, in follow_link

File "build/bdist.linux-x86_64/egg/mechanize/_mechanize.py", line 203, in open

File "build/bdist.linux-x86_64/egg/mechanize/_mechanize.py", line 255, in _mech_open

mechanize._response.httperror_seek_wrapper: HTTP Error 428: Precondition failed

What does that mean? I can't really figure it out. How can I resolve this error?

1

1 Answers

0
votes

As explained here:

The origin server requires the request to be conditional. Its typical use is to avoid the "lost update" problem, where a client GETs a resource's state, modifies it, and PUTs it back to the server, when meanwhile a third party has modified the state on the server, leading to a conflict.

That probably means that the GET parameters in the processed link are suddenly obsolete.