Python 하나의 페이지의 값을 새로고침하여 계속 받아오지만

조회수 1712회

이전에 작성했던 글에서 결국 테이블 값을 받아 왔습니다.

현재 반복적으로 3초마다 해당 하는 함수를 재실행 하고 있습니다.

하지만 새로고침하며 값을 다시 가져오니 처음 한번에는 제대로 작동하지만

두번째 부터는 셀렉트로 값을 특정하여 리스트로 만드는 부분에서 문제가 발생하고 있습니다.

확인결과 전체 데이터를 들고 오는 부분까지는 문제가 없었지만 그 다음 테이블부터 먹통이 되는 문제가 발생합니다.

driver=webdriver.Chrome('C:\Download\webDriber\chromedriver.exe', chrome_options=options)
driver.implicitly_wait(3)

driver.get("http://lin.popall.com/bbs.htm?code=talking&keyfield=&key=&page=1")

def html_parser_func(html):
    global now_column
    global ws

    soup=bs(html, "html.parser")

    # #popclient > table:nth-child(16) > tbody > tr > td:nth-child(6) > table:nth-child(9) nth-of-type
    # 위의 경로로는 []만 가져오는 문제 발생 경로를 XPATH 와 같이하니 정상작동
    post_table=soup.select("#popclient > table:nth-of-type(5) > tbody > tr > td:nth-of-type(6) > table:nth-of-type(6)")[0]

    # 가져온 게시판 table에서 tr요소만 따로 뽑기
    post_list=post_table.select("tr")

    for post in post_list:

        try:
            number=post.select("td")[0].get_text()
            ws.Cells(now_column, 1).Value=number
            print(number)

            title=post.select("td")[1].get_text()
            ws.Cells(now_column, 2).Value=title
            print(title)

            name=post.select("td")[2].get_text()
            ws.Cells(now_column, 3).Value=name
            print(name)

            now = time.localtime()
            ndaytime="%04d.%02d.%02d._%02d:%02d:%02d" % (now.tm_year, now.tm_mon, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec)
            ws.Cells(now_column, 4).Value=now
            print(now)

            now_column = now_column + 1
        except:
            pass

    post_table=[]
    post_list=[]

def refresh_get_table_func(second=1.0):

    global end
    global driver

    print("작동중")

    if end:
        return

    driver.refresh()
    time.sleep(1)
    html=driver.page_source
    html_parser_func(html)

    threading.Timer(second, refresh_get_table_func, [second]).start()


refresh_get_table_func(3.0)
  1. 처음에는 문제없이 제대로 실행되며 값을 찾아오고 리스트로 만듬
  2. 두번째부터 값은 찾아오지만 테이블을 리스트화 하는 부분부터 문제가 발생

문제점을 아시는 고수분이 있다면 도와주시기 바랍니다. 감사합니다.

  • (•́ ✖ •̀)
    알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)