Get Result based on the Top value

Peter Otten __peter__ at web.de
Mon Feb 6 04:16:27 EST 2017


Meeran Rizvi wrote:

> Hello guys,
> Here i am creating a GUI which will act as a search engine.
> When you search for a data in search box it will fetch the results from
> the very first page in browser and it is displayed in a Text widget When
> the TOP value is 10 it should display only the first 10 results based on
> numbers.if it is 20 it should display first 20 results based on the
> number. How should i do that? 

Do you know how to get the integer value from the OptionMenu (or rather the 
associated StringVar)? Use that to check if the current number of results 
(of which you are keeping track already with the v variable) has reached the 
limit. Then use a break statement to exit the for loop.

> Here is my script <python>
> from Tkinter import *
> import mechanize
> from bs4 import BeautifulSoup
> root = Tk()
> 
> def clear_search():#Reset the search field
>     Search.delete(0,END)
>     Result.delete("1.0",END)
> 
> def fun(self=0):
>     new = Search.get()
>     url = "https://duckduckgo.com/"
>     br = mechanize.Browser()
>     br.set_handle_robots(False)
>     br.open(url)
>     br.select_form(name="x")
>     br["q"] = str(new)
>     res = br.submit()
>     content = res.read()
>     #print content
>     soup = BeautifulSoup(content,"html.parser")
>     mylink = soup.find_all('a')
>     v = 0
>     for i in mylink:
>         try:
>             if i.attrs['class'][0] == "result__a":
>                 v = v + 1
>                 Result.insert(END,v)
>                 Result.insert(END,i.text)
>                 Result.insert(END,'\n')
>             elif i.attrs['class'][0] == "result__snippet":
>                 Result.insert(END,i.text)
>                 Result.insert(END,'\n')
>                 Result.insert(END,i.attrs['href'])
>                 Result.insert(END,'\n ')
>                 
Result.insert(END,'--------------------------------------------------------------------------')
>               
>         except KeyError:
>             pass
>             
>             
>     with open("result1.xls", "w") as f:
>         f.write(content)
>       
> Value = Label(root,text="Value:", font="-weight bold")
> Value.grid(row=0,column=0,padx=15)
> 
> Search = Entry(root,width=50)
> Search.grid(row=0,column=1)
> 
> Top = Label(root,text="TOP",font="-weight bold")
> Top.grid(row=1,column=0)
> 
> var = StringVar(root)
> var.set('10')
> 
> Dropdownlist = OptionMenu(root,var,'10','20','50','100')
> Dropdownlist.grid(row=1,column=1,sticky="w")
> 
> 
> Go = Button(root,text="GO",width=5,command=fun)
> Go.bind("<Key>",fun)
> Go.grid(row=1,column=1)
> 
> 
> 
> 
> Reset = Button(root,text="RESET",width=5,command=clear_search)
> Reset.grid(row=1,column=1,sticky="e")
> 
> Result = Text(root,height=20,width=75)
> Result.grid(row=2,column=1,padx=50,pady=10)
> Scroll = Scrollbar(root,command=Result.yview)
> Scroll.grid(row=2,column=2,sticky=N+S)
> Result.config(yscrollcommand=Scroll.set)
> 
> root.mainloop()
> 
> </python>





More information about the Python-list mailing list