Python Selenium(Visual Studio Code) 사용

참조: https://yasulab-pg.com/77-2/

pip를 최신판으로 만들었어요.


hoge.py
python -m pip install --upgrade pip

selenium 설치


Visual Studio Code의 [Terminal] - [New Teaminal]에서 PowerShell 실행
hoge.py
pip install selenium

BeautifulSoup 설치


hoge.py
pip install beautifulsoup4

lxml 패키지 설치


hoge.py
pip install lxml

Requests 패키지 설치


hoge.py
pip install requests

cherome driver 다운로드


도움말 - 크롬 정보

https://sites.google.com/a/chromium.org/chromedriver/downloads

다운로드 & 해동 후 실행 파일과 같은 곳에 설정

Selenium의 기본 문법


hoge.py
from selenium import webdriver
from bs4 import BeautifulSoup


options = webdriver.ChromeOptions()

#headlessならflg=1とする
flg=1

if flg==1:
  options.add_argument('--headless')
  options.add_argument('--no-sandbox')
  options.add_argument('--disable-dev-shm-usage')

driver = webdriver.Chrome('chromedriver',options=options)

url="https://kaikan.co/shop/kinki/?p=2"

driver.get(url)

html = driver.page_source.encode('utf-8')
soup = BeautifulSoup(html, 'lxml')

results = soup.find_all("h2", class_="_shop_name")

cell_list=[]

# 結果を出力

#2秒待つ
time.sleep(2)

for result in results:

    txt = result.findAll("a")[0].get_text()
    href= result.findAll("a")[0].get('href')

    #cell_list.append(txt)
    #cell_list.append(href)

    print(txt+','+href)

driver.close()
driver.quit()

hoge.py
hoge=driver.find_element_by_id("ex13")
hoge=driver.find_element_by_name("b2")
hoge=driver.find_elements_by_link_text('あいうえお')
hoge=driver.find_elements_by_xpath(".//a")

#クリック処理
element=driver.find_element_by_class_name('ticketSelect__actionLabel')
element.click()

#ドロップダウン処理
dropdown = driver.find_element_by_name('Y15-seatdata-seat_size_1')
select_element = Select(dropdown)
select_element.select_by_index(1)

BeautifulSoup


hoge.py
#BeautifulSoup
hoge=soup.findAll(class_="p-articles-list-item")
hoge=soup.findAll("a")[0].get('href')
hoge=soup.find(class_='p-item-most-important').get_text()
hoge=soup.find(class_='p-item-title').find("a").get('href')

좋은 웹페이지 즐겨찾기