Get Result based on the Top value

Meeran Rizvi hmmeeranrizvi18 at gmail.com
Mon Feb 6 05:53:50 EST 2017


On Friday, February 3, 2017 at 5:35:13 PM UTC+5:30, 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?
> 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>

Here i got it,using [var.get()]
When the Top value is change to 10 it displays only the first 10 results,if it is changed to 20 it displays first 20 results.But i got a problem with my GUI the scroll bar height is not same as my Text widget.even i used sticky its height remains to be short only.

<python>
from Tkinter import *
import mechanize
from bs4 import BeautifulSoup
root = Tk()

options = ["10","20","30"]

var = StringVar(root)
var.set('10')

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 = 1
    b = var.get()
    
    for i in mylink:
        try:
             
             if v <= int(b):
                
                if i.attrs['class'][0] == "result__a":
                    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,'--------------------------------------------------------------------')
                    v = v + 1
        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,sticky="W")

Search = Entry(root,width=50)
Search.grid(row=0,column=1)

Top = Label(root,text="TOP",font="-weight bold")
Top.grid(row=0,column=2,sticky="W")


Go = Button(root,text="GO",width=5,command=fun)
Go.bind("<Key>",fun)
Go.grid(row=0,column=4,sticky="W")

Dropdownlist = OptionMenu(root,var,*options)
Dropdownlist.grid(row=0,column=3,padx=5,sticky="W")

Reset = Button(root,text="RESET",width=5,command=clear_search)
Reset.grid(row=0,column=5,padx=5,sticky="W")

Result = Text(root,height=20,width=69)
Result.place(x=10, y=40)
Scroll = Scrollbar(root,command=Result.yview)
Scroll.grid(row=1,column=6,sticky=N)
Result.config(yscrollcommand=Scroll.set)


root.mainloop()

</python>



More information about the Python-list mailing list