0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Python Selenium を使ってみる(VisualStudio Code)

Last updated at Posted at 2021-04-12

参考:https://yasulab-pg.com/77-2/

###pipを最新版にする

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

##seleniumをインストールする
VisualStudioCodeの[Terminal]-[NewTeaminal]でPowerShellを実行する

hoge.py
pip install selenium

###BeautifulSoupをインストールする

hoge.py
pip install beautifulsoup4

###lxmlパッケージをインストールする

hoge.py
pip install lxml

###requestsパッケージをインストールする

hoge.py
pip install requests

##chrome driverのダウンロード
ヘルプ-Chromeについて
image.png

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

ダウンロード&解凍の後、実行ファイルと同じ場所に設置
image.png

##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')

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?