2011-07-30 19 views
0

私はこのレポートを使って、年次報告書(10-K's)のSEC Edgarデータベースを検索し、リストボックスに40種類の項目のリストを返します。さて、私は次のコードが実現リストボックスに次の40個の項目を表示し、[次へ40 'ボタンを作成したい:Buttonコマンドの問題Tkinter Python

def Next(): 

global entryWidget 

page = 'http://www.sec.gov/cgi-bin/browse-edgar?company=&match=&CIK=' + entryWidget.get().strip() + '&filenum=&State=&Country=&SIC=&owner=exclude&Find=Find+Companies&action=getcompany' 
sock = urllib.urlopen(page) 
raw = sock.read() 
soup = BeautifulSoup(raw) 

npar = str(soup.find(value="Next 40")) 
index = npar.find('/cgi') 
index2 = npar.find('count=40') + len('count=40') 
nextpage = 'http://www.sec.gov' + npar[index:index2] 

sock2 = urllib.urlopen(nextpage) 
raw2 = sock2.read() 
soup2 = BeautifulSoup(raw2) 

psoup = str(soup2.findAll(nowrap=True)) 

myparser = MyParser() 
myparser.parse(psoup) 

filinglist = myparser.get_descriptions() 
linklist = myparser.get_hyperlinks() 

filinglist = [s for s in filinglist if s != 'Documents'] 
filinglist = [s for s in filinglist if s != 'Documents Interactive Data'] 
filinglist = [s for s in filinglist if not re.match(r'\d{3}-', s)] 

linklist = [s for s in linklist if not s.startswith('/cgi-')] 

Lb1.delete(0, END) 

counter = 0 

while counter < len(filinglist): 
    Lb1.insert(counter, filinglist[counter]) 
    counter = counter +1 

ボタンが押されたときにあなたが見ることができるように、それは(元のリンクを読み込みページ)よりも、「次の40」のハイパーリンクを探します。次に、新しいHTML文書(nextpage)を解析し、続いて項目名と関連リンクを取得します。これで、このコードは元のページの次のページに移動しますが、次のページを表示することはできません。

(nextpage)をオリジナル(ページ)に作成し、「次へ」ボタンを押すたびに(nextnextpage)htmlドキュメントから項目をリストすることができるのはどうですか?申し訳ありませんが混乱している場合、私は実際にそれを説明するための他の方法を知っていません。

実際のサイトリンクは次のとおりです。 ... getcompany 「次へ」ボタンをクリックして、そのサイトの「次の40」ボタンからHTMLハイパーリンクを取得します。ここで

は、あなたがそれを必要とする場合は私の全体のプログラムコードです:

import BeautifulSoup 
from BeautifulSoup import BeautifulSoup 
import urllib 
import sgmllib 
from Tkinter import * 
import tkMessageBox 
import re 

class MyParser(sgmllib.SGMLParser): 

def parse(self, psoup): 
    self.feed(psoup) 
    self.close() 

def __init__(self, verbose=0): 
    sgmllib.SGMLParser.__init__(self, verbose) 
    self.descriptions = [] 
    self.hyperlinks = [] 
    self.inside_td_element = 0 
    self.starting_description = 0 

def start_td(self, attributes): 
    for name, value in attributes: 
     if name == "nowrap": 
      self.inside_td_element = 1 
      self.starting_description = 1 

def end_td(self): 
    self.inside_td_element = 0 

def start_a(self, attributes): 
    for name, value in attributes: 
     if name == "href": 
      self.hyperlinks.append(value) 

def handle_data(self, data): 
    if self.inside_td_element: 
     if self.starting_description: 
      self.descriptions.append(data) 
      self.starting_description = 0 
     else: 
      self.descriptions[-1] += data 

def get_descriptions(self): 
    return self.descriptions 

def get_hyperlinks(self): 
    return self.hyperlinks 

def Submit(): 

global entryWidget 

if entryWidget.get().strip() == "": 
    tkMessageBox.showerror("Tkinter Entry Widget", "Enter a text value") 
else: 
    page = 'http://www.sec.gov/cgi-bin/browse-edgar?company=&match=&CIK=' + entryWidget.get().strip() + '&filenum=&State=&Country=&SIC=&owner=exclude&Find=Find+Companies&action=getcompany' 
    sock = urllib.urlopen(page) 
    raw = sock.read() 
    soup = BeautifulSoup(raw) 
    psoup = str(soup.findAll(nowrap=True)) 
    myparser = MyParser() 
    myparser.parse(psoup) 

    filinglist = myparser.get_descriptions() 
    linklist = myparser.get_hyperlinks() 

    filinglist = [s for s in filinglist if s != 'Documents'] 
    filinglist = [s for s in filinglist if s != 'Documents Interactive Data'] 
    filinglist = [s for s in filinglist if not re.match(r'\d{3}-', s)] 

    linklist = [s for s in linklist if not s.startswith('/cgi-')] 

    counter = 0 

    while counter < len(filinglist): 
     Lb1.insert(counter, filinglist[counter]) 
     counter = counter +1 

    downloadbutton.configure(state=NORMAL) 
    nextbutton.configure(state=NORMAL) 

def Next(): 

global entryWidget 

page = 'http://www.sec.gov/cgi-bin/browse-edgar?company=&match=&CIK=' + entryWidget.get().strip() + '&filenum=&State=&Country=&SIC=&owner=exclude&Find=Find+Companies&action=getcompany' 
sock = urllib.urlopen(page) 
raw = sock.read() 
soup = BeautifulSoup(raw) 

npar = str(soup.find(value="Next 40")) 
index = npar.find('/cgi') 
index2 = npar.find('count=40') + len('count=40') 
nextpage = 'http://www.sec.gov' + npar[index:index2] 

sock2 = urllib.urlopen(nextpage) 
raw2 = sock2.read() 
soup2 = BeautifulSoup(raw2) 

psoup = str(soup2.findAll(nowrap=True)) 

myparser = MyParser() 
myparser.parse(psoup) 

filinglist = myparser.get_descriptions() 
linklist = myparser.get_hyperlinks() 

filinglist = [s for s in filinglist if s != 'Documents'] 
filinglist = [s for s in filinglist if s != 'Documents Interactive Data'] 
filinglist = [s for s in filinglist if not re.match(r'\d{3}-', s)] 

linklist = [s for s in linklist if not s.startswith('/cgi-')] 

Lb1.delete(0, END) 

counter = 0 

while counter < len(filinglist): 
    Lb1.insert(counter, filinglist[counter]) 
    counter = counter +1 

previousbutton.configure(state=NORMAL) 
nextbutton.configure(state=DISABLED) 

def Previous(): 

global entryWidget 

page = 'http://www.sec.gov/cgi-bin/browse-edgar?company=&match=&CIK=' + entryWidget.get().strip() + '&filenum=&State=&Country=&SIC=&owner=exclude&Find=Find+Companies&action=getcompany' 
sock = urllib.urlopen(page) 
raw = sock.read() 
soup = BeautifulSoup(raw) 

psoup = str(soup.findAll(nowrap=True)) 

myparser = MyParser() 
myparser.parse(psoup) 

filinglist = myparser.get_descriptions() 
linklist = myparser.get_hyperlinks() 

filinglist = [s for s in filinglist if s != 'Documents'] 
filinglist = [s for s in filinglist if s != 'Documents Interactive Data'] 
filinglist = [s for s in filinglist if not re.match(r'\d{3}-', s)] 

linklist = [s for s in linklist if not s.startswith('/cgi-')] 

Lb1.delete(0, END) 

counter = 0 

while counter < len(filinglist): 
    Lb1.insert(counter, filinglist[counter]) 
    counter = counter +1 

nextbutton.configure(state=NORMAL) 
previousbutton.configure(state=DISABLED) 

if __name__ == "__main__": 

root = Tk() 
root.title("SEC Edgar Search") 
root["padx"] = 10 
root["pady"] = 25 

top = Frame(root) 
bottom = Frame(root) 
bottom2 = Frame(root) 
top.pack(side=TOP) 
bottom.pack(side=BOTTOM, fill=BOTH, expand=True) 
bottom2.pack(side=BOTTOM, fill=BOTH, expand=True) 

textFrame = Frame(root) 

entryLabel = Label(textFrame) 
entryLabel["text"] = "Ticker symbol:" 
entryLabel.pack(side=TOP) 

entryWidget = Entry(textFrame) 
entryWidget["width"] = 15 
entryWidget.pack(side=LEFT) 

textFrame.pack() 

scrollbar = Scrollbar(root) 
scrollbar.pack(side=RIGHT, fill=Y) 

Lb1 = Listbox(root, width=20, height=15, yscrollcommand=scrollbar.set, selectmode=EXTENDED) 
Lb1.pack() 

scrollbar.config(command=Lb1.yview) 

submitbutton = Button(root, text="Submit", command=Submit) 
submitbutton.pack(in_=bottom2, side=TOP) 

downloadbutton = Button(root, text="Download") 
downloadbutton.pack(in_=bottom2, side=TOP) 
downloadbutton.configure(state=DISABLED) 

previousbutton = Button(root, text="Previous 40", command=Previous) 
previousbutton.pack(in_=bottom, side=LEFT) 
previousbutton.configure(state=DISABLED) 

nextbutton = Button(root, text="Next 40", command=Next) 
nextbutton.pack(in_=bottom, side=LEFT) 
nextbutton.configure(state=DISABLED) 

root.mainloop() 

答えて

1

ではなく、グローバルのApplicationクラスを使用します。現在、常に最初のページをダウンロードしています。 onClickのリンクが&スタートを更新

class Application(Frame): 
    def __init__(self, parent=None): 
     Frame.__init__(self, parent) 
     self.pack() 

     self.top = Frame(self) 
     self.bottom = Frame(self) 
     self.bottom2 = Frame(self) 
     self.top.pack(side=TOP) 
     self.bottom.pack(side=BOTTOM, fill=BOTH, expand=True) 
     self.bottom2.pack(side=BOTTOM, fill=BOTH, expand=True) 
     #... 
     self.submitbutton = Button(self, text="Submit", command=self.submit) 
     self.submitbutton.pack(in_=self.bottom2, side=TOP) 
     #... 

    #... 

    def submit(self): 
     page = ('http://www.sec.gov/cgi-bin/browse-edgar?company=&match=&CIK=' + 
       self.entryWidget.get().strip() + 
       '&filenum=&State=&Country=&SIC=&owner=exclude' 
       '&Find=Find+Companies&action=getcompany') 
     #... 
     self.soup = ... 

    def next(self): 
     #... 
     #there must be a better way than this to extract the onclick value 
     #but I don't use/know BeautifulSoup to help with this part 

     npar = str(self.soup.find(value="Next 40")) 
     index1 = npar.find('/cgi') 
     index2 = npar.find('count=40') + len('count=40') 
     page = 'http://www.sec.gov' + npar[index1:index2] 

     sock = urllib.urlopen(page) 
     raw = sock.read() 
     self.soup = BeautifulSoup(raw) 

     #... 

if __name__ == '__main__': 
    root = Tk() 
    root.title("SEC Edgar Search") 
    root["padx"] = 10 
    root["pady"] = 25 

    app = Application(root) 

    app.mainloop() 
    root.destroy() 

それぞれの新しいページの場合:しかし、あなたのアプリケーションクラスはnextは、「次の40」フォームボタンからのonClick値を取得するために使用する現在のページの「スープ」をキャッシュする必要がありますパラメータ。そうする代わりに、現在のスープを解析して値を取得することなく、クラス内のカウンタをインクリメントすることができます。

+0

次のコードを使用して新しいクラスを作成しようとしています。クラスApplication():def submit(self):。 。 。等。しかし、私はこの例外を受け取り続けています: トレースバック(最新の最後の呼び出し): ファイル "C:\ Python27 \ lib \ lib \ tk \ Tkinter.py"、行1410、__call__ return self.func * args) TypeError:バインドされていないメソッドSubmit()は、アプリケーションインスタンスで最初の引数として呼び出される必要があります(何もありません)。これを引き起こす原因は何ですか? – kr21

+0

はい、これは完璧に感謝します! – kr21

関連する問題