블로그 이미지
훅크선장

카테고리

분류 전체보기 (362)
사진이야기 (23)
펭귄컴퓨팅 (121)
컴퓨터보안 (84)
절름발이 프로그래머 (59)
하드웨어개조 (23)
멀알려줄까 (35)
홈베이킹&홈쿠킹 (2)
잡다한것들 (15)
Total
Today
Yesterday

달력

« » 2024.5
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

공지사항

태그목록

최근에 올라온 글

"""
블로그에 있는 글의 제목들을 추출하는 xpath 구문
page.locator("//div[@class='entry']/div[@class='titleWrap']/h2/a").all_text_contents()

블로그에 있는 글의 작성일자를 추출하는 xpath 구문
page.locator("//div[contains(@class,'entry')]/div[@class='titleWrap']/div[@class='info']/span[@class='date']").first.text_content()
page.locator("//div[@class='entry']/div[@class='titleWrap']/div[@class='info']/span[@class='date']").last.text_content()
page.locator("//div[@class='entry']/div[@class='titleWrap']/div[@class='info']/span[@class='date']").all_text_contents()

블로그에 있는 글의 작성일자를 추출하는 css 구문
page.locator('div:nth-child(3) > div.titleWrap > div > span.date').text_content()

블로그에 있는 글의 개별 링크 URL들을 추출하는 xpath 구문
link_locators = page.locator("//div[@class='entry']/div[@class='titleWrap']/h2/a").all()
for l_loc in link_locators:
print(l_loc.get_attribute('href'))
print(l_loc.text_content())

※ locator() 내부에서 명시적으로 css=   xpath= 를 삽입할 수 있지만, 꼭 쓸 필요는 없다.
page.locator("xpath=/html/body/div[1]/div/div[2]/div[2]/div/div[3]/div[1]/div/span[2]").text_content()
"""


from playwright.sync_api import Playwright, sync_playwright, expect
import os
from datetime import datetime

def run(playwright: Playwright) -> None:

  # Get Current Working Directory
  current_dir = os.getcwd()
  if current_dir[-1] != '/':
  current_dir = current_dir + '/'
  #print(current_dir)

  # Get Current Date and Time
  current_datetime = datetime.now().strftime("%Y-%m-%d %H-%M-%S")
  #print("Current Date & Time : ", current_datetime)
  # Convert datetime obj to string
  str_current_datetime = str(current_datetime)

  ## 브라우저가 화면에 나타나지 않도록 headless옵션을 켜고, 크롬브라우저를 사용합니다.
  ## headless=False 이면, 브라우저가 화면에 나타납니다.
  browser = playwright.chromium.launch(headless=True, channel="chromium")
  context = browser.new_context()

  ## 브라우저로 웹페이지를 실행합니다
  page = context.new_page()

  ## 아래 URL 주소로 이동합니다.
  page.goto("https://hook.tistory.com/")

  ## 웹 페이지의 스크린샷을 뜬다. path= 파라미터를 사용하여 저장경로를 별도로 지정한다.
  #page.screenshot(path=current_dir + 'capture/'+ f'screenshot-{str_current_datetime}.png')

  entry_locators = page.locator("//div[@class='entry']/div[@class='titleWrap']").all()
  for a_loc in entry_locators:
    print(a_loc.locator("//h2/a").text_content())
    print(a_loc.locator("//div[@class='info']/span[@class='date']").text_content())
    print(a_loc.locator("//h2/a").get_attribute('href'))
    print("----------------------------------------------------------------------------------------")

  ## 잠시 중지
  #page.pause()

  # 브라우저 종료
  context.close()
  browser.close()

# 주 실행함수 
with sync_playwright() as playwright:
    run(playwright)

Posted by 훅크선장
, |