Rédirection I/O vers un fichier qui échoue "de temps en temps"
Bonjour,
je suis en train écrire un script pour chercher les données concernant un DVD depuis le site de la fnac.
Ce qu'il est censé faire est :
1) il ouvre une fenêtre avec une zone de saisie où on peut saisir
des mots clé
2) quand on clique sur le bouton "Search", il cherche des titres depuis le site de la fnac, et ouvre une nouvelle fenêtre en proposant le choix des titres.
3) quand on clique sur le titre choisi, il récupère les informations,
les écrits sur le fichier /var/www/fnactest.html
4) il lance opera pour ouvrir l'url "localhost/fnactest.html
Voici le code.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| #! /usr/bin/env python
# -*- coding: utf-8 -*-
import Pmw
import urllib2
import re
import sys
import os
urlroot = 'http://recherche.fnac.com/Search/SearchResult.aspx?SCat=4!1&Search='
urlend = '&sft=1&submitbtn=OK'
Browserpath = '/usr/bin/opera'
from Tkinter import *
root = Tk()
master = Frame(root)
master.pack()
e = Entry(master)
e.pack()
e.focus_set()
answer = ""
def recup(adrs):
print "retrieving informations from"
print adrs
f=open('/var/www/fnactest.html','w')
start=0
for line in urllib2.urlopen(adrs):
if "données détaillée de l'article" in line:
start=1
print "we have found the begining of the table"
if start > 0:
print line
f.write(line)
if '</table' in line:
f.close
break
os.execl(Browserpath,'localhost/fnactest.html')
def callback(tag):
# This is called whenever the user clicks on a
# button in the RadioSelect widget.
print "function recup called"
print dic[tag]
recup( dic[tag])
def menu(dic):
# for k,v in dic.iteritems():
# print k,v
# for text in dic.keys:
# radio.add(text)
# Initialise Tkinter and Pmw.
root = Pmw.initialise(fontScheme = 'pmw1')
root.title('Click on the title')
# Create and pack a RadioSelect widget.
radio = Pmw.RadioSelect(
command = callback,
labelpos = 'w',
orient = 'vertical')
radio.pack(padx = 20, pady = 20)
# Add some buttons to the RadioSelect.
for k,v in dic.iteritems():
radio.add(k)
# radio.invoke('Vegetables')
# Create an exit button.
exit = Button(text = 'Cancel', command = root.destroy)
exit.pack(pady = 20)
def printanswer():
global answer
global dic
answer = e.get()
url = urlroot + answer + urlend
dic={}
previous=0
current=0
# f=open('/var/www/fnactest.html','w')
for line in urllib2.urlopen(url):
if previous > 0 :
current=1
previous=0
if '"lienInverse title"' in line:
previous = 1
#if 'google_adtest' in line:
# break
if current > 0:
if '?' in line:
newline=line.split('?')
val=newline[0].replace('<a href="','',1)
splitfurther=newline[1].split('>')
key=splitfurther[1].replace('</a','')
dic[key]=val
else:
newline=line.split('" class')
val=newline[0].replace('<a href="','',1)
splitfurther=newline[1].split('>')
key=splitfurther[1].replace('</a','')
#print(val)
#print(key)
#f.write(line)
#f.write("<br>")
current=0
#Now that the dictionary is created, we show the second menu
#Might be neeter if we define the second menu as a function
root.destroy()
menu(dic)
#f.close()
# print answer
# b1 = Button(master, text="SET", width=10, command=setanswer)
# b1.pack()
b2 = Button(master, text="Search", width=10, command=printanswer)
b2.pack()
root.mainloop() |
Or, de temps en temps, ça ne marche pas. Par exemple, si je fais chercher d'abord le mot-clé "fille" puis choisis "La fille a valise", ça marche parfaitement, tandisque si je choisis le DVD "Jamais sans ma fille", la sortie sur le terminal est correcte (ligne 33 du code), mais il n'y a rien dans le fichier
/var/www/fnactest.html (ligne34 du code).
On peut constater que la fonction "recup" est appelée avec bon argument (les lignes 43 et 44).
Par ailleurs on peut aussi voir que "recup" fait ce qu'elle devrait, c'est-à dire, si je fais
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
import re
import sys
import os
urlgood="http://video.fnac.com/a2230121/La-Fille-a-la-valise-Claudia-Cardinale-DVD-Zone-2"
urlbad="http://video.fnac.com/a1616796/Jamais-sans-ma-fille-Sally-Field-DVD-Zone-2"
choice=[urlgood,urlbad]
def recup(adrs):
print "retrieving informations from"
print adrs
f=open('/var/www/fnactest.html','w')
start=0
for line in urllib2.urlopen(adrs):
if "données détaillée de l'article" in line:
start=1
print "we have found the begining of the table"
if start > 0:
print line
f.write(line)
if '</table' in line:
f.close
break
recup(choice[int(sys.argv[1])]) |
cette fois, j'ai droit au ficher /var/www/fnactest.html comme attendu.
Je ne sais pas où je pourrais chercher des erreurs. Quelqu'un pourrait
m'aider à debugger ce code s'il vous plait? Merci d'avance.
Encore des mystères. Toutes les fonctions marchent individuellement mais pas ensemble
Je continue le debugging, et j'ai fait le code suivant. Cette fois-ci, il n'y a plus de menu du tout, il cherche donc tout seul les informations sur les 15 (premiers) dvd ayant "fille"
dans le titre ou mot-clé. Et là, le script fait exactement ce qu'il doit. Ce qui confirme que la fonction "printanswer" passe des arguments correctes à la fonction menu()
(qui est remplacée par la fonction newmenu() dans le nouveau code pour le debugging). Or, la différence entre la fonction menu() et newmenu(), c'est just le gui pour choisir un titre parmi les 15... Et sans doute le fait que dans
la fonction "newmenu" j'utilise la variable "v" au lieu de
"dic(k)", mais c'est pareil... Quelqu'un pourrait m'aider s'il vous plaît?
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import Pmw
import urllib2
import re
import sys
import os
urlroot = 'http://recherche.fnac.com/Search/SearchResult.aspx?SCat=4!1&Search='
urlend = '&sft=1&submitbtn=OK'
Browserpath = '/usr/bin/opera'
from Tkinter import *
root = Tk()
master = Frame(root)
master.pack()
e = Entry(master)
e.pack()
e.focus_set()
answer = ""
def newmenu(dic): #function introduced for debugging purpose only
j=0
for k,v in dic.iteritems():
print k
print v
f=open('/var/www/fnactest'+str(j)+ '.html','w')
start=0
for line in urllib2.urlopen(v):
if "données détaillée de l'article" in line:
start=1
print "we have found the begining of the table"
if start > 0:
#print line
f.write(line)
if '</table' in line:
f.close
break
j=j+1
def printanswer(): #this function searches for dvd's with "answer" in keywords, store the information into a dictionary, and call the function menu
global answer
global dic
#answer = e.get()
answer="fille"
url = urlroot + answer + urlend
dic={}
previous=0
current=0
# f=open('/var/www/fnactest.html','w')
for line in urllib2.urlopen(url):
if previous > 0 :
current=1
previous=0
if '"lienInverse title"' in line:
previous = 1
#if 'google_adtest' in line:
# break
if current > 0:
if '?' in line:
newline=line.split('?')
val=newline[0].replace('<a href="','',1)
splitfurther=newline[1].split('>')
key=splitfurther[1].replace('</a','')
dic[key]=val
else:
newline=line.split('" class')
val=newline[0].replace('<a href="','',1)
splitfurther=newline[1].split('>')
key=splitfurther[1].replace('</a','')
#print(val)
#print(key)
#f.write(line)
#f.write("<br>")
current=0
#Now that the dictionary is created, we show the second menu
#Might be neeter if we define the second menu as a function
root.destroy()
# menu(dic) commented out for debugging
newmenu(dic)
#f.close()
# print answer
# b1 = Button(master, text="SET", width=10, command=setanswer)
# b1.pack()
#b2 = Button(master, text="Search", width=10, command=printanswer)
#b2.pack()
#answer="fille"
printanswer()
root.mainloop() |