2011-07-30 7 views
0

그래서이 프로그램은 SEC 보고서를 SEC 에드거 데이터베이스에서 검색하여 목록 상자에 40 개의 다른 항목 목록을 반환합니다. 원래 링크 (읽기, 버튼을 누를 때버튼 명령의 문제점 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 개 항목을 표시하는 '다음 40'버튼을 만들려면 페이지)에서 "다음 40"하이퍼 링크를 찾습니다. 그런 다음 새 html 문서 (nextpage)를 파싱 한 다음 항목 이름과 관련 링크를 가져옵니다. 이제이 코드는 원본 페이지의 다음 페이지로 이동하지만 한 페이지 만 표시 할 수 있습니다.

그래서 어떻게하면 (nextpage)를 원본 (페이지)으로 만들고 'Next'버튼을 누를 때마다 (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

가 전역 대신 응용 프로그램 클래스를 사용합니다. 현재 항상 첫 페이지를 다운로드하고 있습니다. 온 클릭 링크가 & 시작을 갱신

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"폼 버튼에서 온 클릭 값을 얻기 위해 사용하는 현재 페이지의 '스프'를 캐시한다 매개 변수. 따라서 현재 수프를 구문 분석하여 값을 얻지 않아도 클래스에있는 카운터를 증가시킬 수 있습니다.

+0

다음 코드를 사용하여 새 클래스를 만들려고합니다. Class Application() : def submit (self) :. . .기타. Tkinter 콜백에서 예외가 발생했습니다. Traceback (가장 최근 호출 마지막) : 파일 "C : \ Python27 \ lib \ lib \ tk \ Tkinter.py"파일 1410 줄에서 __call__ return self.func * args) TypeError : 언 바운드 메서드 첫 번째 인수로 Application 인스턴스와 함께 Submit()를 호출해야합니다. 어떤 생각이 원인이 무엇입니까? – kr21

+0

네,이 작품은 완벽하게 감사합니다! – kr21

관련 문제