Post Tagged python 3

Python 3 HTML Parse

Wednesday, 04 April 2011

Sample code for HTML parsing with Python 3.

>>> from html.parser import HTMLParser
>>> parss=HTMLParser()
>>> parss.feed('<a href="http://m-eken.com">link</a>')
>>> parss.get_starttag_text()
'<a href="http://m-eken.com">'
>>>


Treeview in Python Tkinter

Tuesday, 03 March 2010

import tkinter
from tkinter import *
from tkinter import ttk
p=tkinter.Tk()
t=ttk.Treeview(p)
t["columns"]=("first","second")
t.column("first",width=75,anchor="center" )
t.column("second",width=100)
t.heading("first",text="first column")
t.heading("second",text="second column")
t.insert("",0,"dir1",text="directory 1")
t.insert("dir1","end","dir 1",text="file 1 1",values=("file 1 A","file 1 B"))
id=t.insert("","end","dir2",text="directory 2")
t.insert("dir2","end",text="dir 2",values=("file 2 A","file 2 B"))
t.insert(id,"end",text="dir 3",values=("val 1 ","val 2"))
t.insert("",0,text="first line",values=("first line 1","first line 2"))
t.tag_configure("ttk")
t.pack(side=BOTTOM,fill=X)
p.mainloop()


Google Search with Python 3 and Tkinter

Monday, 02 February 2010

Google Search with Python and Tkinter;

# http://m-eken.com
# python 3
#google Searcher
import tkinter,urllib.request,urllib.parse,urllib.error
from tkinter import *
from tkinter import ttk

class window:
    def __init__(self,root):
        self.root=root
        self.root.title("Google Searcher")
        ttk.Frame(self.root, width=500,height=250).pack()
        self.init_widgets()

    def init_widgets(self):
        ttk.Button(self.root, command=self.insert_text,text="Gooo",width="10").place(x=400,y=10)
        self.txt=tkinter.Text(self.root,width="52",height="10", font=12)
        self.txt.place(x=10,y=40)
        self.searchWord=tkinter.Text(self.root,width="42",height="1", font=12)
        self.searchWord.insert(tkinter.INSERT,"google search with python 3 tkinter")
        self.searchWord.place(x=10,y=15)
 

    def insert_text(self):
        GWord=self.searchWord.get(1.0,END)
        url="http://www.google.com/search?hl=en&q="+"+".join(GWord.split())
        req=urllib.request.Request(url)
        req.add_header("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.29 Safari/525.13")
        GWord= urllib.request.urlopen(req).read()
        self.txt.insert(tkinter.INSERT,  GWord)
         

if __name__=="__main__":
    root=tkinter.Tk()
    window(root)
    root.mainloop()

Python 3 Google Search Sample

Wednesday, 02 February 2010

Sample Google Search Python code
import urllib.request,urllib.parse,urllib.error
print("Google Searcher --- www.m-eken.com v.P5012")
while input("s: Search. q: Quit #: ")=="s":
GWord=input("#: ")
url="http://www.google.com/search?hl=en&q="+"+".join(GWord.split())
print(GWord+"\nsearching... ")
req=urllib.request.Request(url)
req.add_header("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/525.13 (KHTML,     like Gecko) Chrome/0.2.149.29 Safari/525.13")
print( urllib.request.urlopen(req).read())