LoginSignup
2

More than 5 years have passed since last update.

WebDriverでアクセスしたWebサイトのソースコードを取り出したい

Posted at

WebDriverでアクセスしたWebサイトのソースコード(html)を取り出したいという場合は、webdriverpage_source属性を利用すればよいようです。以下はWebDriverでアクセスしたサイトのソースコードをBeautifulSoupに食わせるサンプルコードになります。

from selenium import webdriver
from bs4 import BeautifulSoup


if __name__ == '__main__': 
    driver = webdriver.Firefox()
    driver.get('https://helloworld.jp/hello/world')
    soup = BeautifulSoup(driver.page_source, 'html.parser')
    for div in soup.find_all('div'):
        do_someting(div)
    driver.quit()

参考: http://selenium-python.readthedocs.io/api.html

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
2