コミートム合同会社

コミートム合同会社

ウェブスクレイピングの始め方

GoogleseleniumprintThis article provides a stepbystep guide on how to get started with web scraping using Python. The guide covers installing the necessary software, creating a virtual environment, installing Selenium, downloading the Chrome driver, and writing a Python script to search Google for the term python selenium.
by JanitorJun 18, 2023
情報収集や事務作業、発注作業の自動化などを目的としてウェブスクレイピングを始めようとされている方に向けた情報です。 バージョンが変わったりなどして始めにくいような状況かと思いますが、最初の一歩として、Googleで検索をその結果をprintします。 環境は、Windows11、Chrome、Visual Studio Code、pythonです。
情報収集や事務作業、発注作業の自動化などを目的としてウェブスクレイピングを始めようとされている方に向けた情報です。 バージョンが変わったりなどして始めにくいような状況かと思いますが、最初の一歩として、Googleで検索をその結果をprintします。 環境は、Windows11、Chrome、Visual Studio Code、pythonです。

Jump Links

1.仮想環境作成用のvirtualenvをインストール
2.仮想環境作成
3.仮想環境に入ってseleniumをインストール
4.Chromeドライバーをダウンロード
5.Pythonファイルを作成
6.結果
おわりに
1.仮想環境作成用のvirtualenvをインストール
1.仮想環境作成用のvirtualenvをインストール
コマンドプロンプトで下記コマンドを実行します。
コマンドプロンプトで下記コマンドを実行します。
pip install virtualenv
pip install virtualenv
2.仮想環境作成
2.仮想環境作成
仮想環境作成のためのコマンドを入力します。 環境名は、「testEnv」としています。
仮想環境作成のためのコマンドを入力します。 環境名は、「testEnv」としています。
python -m venv testEnv
python -m venv testEnv
3.仮想環境に入ってseleniumをインストール
3.仮想環境に入ってseleniumをインストール
仮想環境を作成したフォルダで下記コマンドを実行します。 seleniumのインストールされたバージョンは、「4.10.0」でした。 最初のコマンドが作成した仮想環境に入るコマンドです。 出るときは、「deactivate」を実行すると出られます。
仮想環境を作成したフォルダで下記コマンドを実行します。 seleniumのインストールされたバージョンは、「4.10.0」でした。 最初のコマンドが作成した仮想環境に入るコマンドです。 出るときは、「deactivate」を実行すると出られます。
testEnv\Scripts\activate pip install selenium pip install selenium-wire pip list (中略) selenium 4.10.0 (後略)
testEnv\Scripts\activate pip install selenium pip install selenium-wire pip list (中略) selenium 4.10.0 (後略)
4.Chromeドライバーをダウンロード
4.Chromeドライバーをダウンロード
下記のURLからドライバーをダウンロードして解凍します。 Windowsの場合は、「chromedriver_win32.zip」を選択してください。 解凍すると中身は、「chromedriver.exe」というexeで、好きなフォルダにおいてください。 あとで、その場所をpythonファイルに書きます。 https://chromedriver.chromium.org/downloads
下記のURLからドライバーをダウンロードして解凍します。 Windowsの場合は、「chromedriver_win32.zip」を選択してください。 解凍すると中身は、「chromedriver.exe」というexeで、好きなフォルダにおいてください。 あとで、その場所をpythonファイルに書きます。 https://chromedriver.chromium.org/downloads
5.Pythonファイルを作成
5.Pythonファイルを作成
pythonファイル、launch.jsonにそれぞれ下記のように書き込みます。 実行時の引数は「python selenium」としていて、seleniumを使って、「python selenium」をgoogleで検索させています。
pythonファイル、launch.jsonにそれぞれ下記のように書き込みます。 実行時の引数は「python selenium」としていて、seleniumを使って、「python selenium」をgoogleで検索させています。
pythonファイル import sys from seleniumwire import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.proxy import Proxy, ProxyType from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.keys import Keys googleUrl = "https://google.com/search?q=" searchWords = [] def main(): service = Service(executable_path='chromedriver.exeへのパス') driver = webdriver.Chrome(service=service) searchWordsStr = "" if( len( searchWords ) > 0 ): searchWordsStr = '+'.join( searchWords[ 1 : ] ) else: return driver.implicitly_wait( 10 ) driver.get( 'https://google.com/search?q=' + searchWordsStr ) print( driver.page_source ) if __name__ == '__main__': searchWords = sys.argv main()
pythonファイル import sys from seleniumwire import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.proxy import Proxy, ProxyType from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.keys import Keys googleUrl = "https://google.com/search?q=" searchWords = [] def main(): service = Service(executable_path='chromedriver.exeへのパス') driver = webdriver.Chrome(service=service) searchWordsStr = "" if( len( searchWords ) > 0 ): searchWordsStr = '+'.join( searchWords[ 1 : ] ) else: return driver.implicitly_wait( 10 ) driver.get( 'https://google.com/search?q=' + searchWordsStr ) print( driver.page_source ) if __name__ == '__main__': searchWords = sys.argv main()
launch.json { "version": "0.2.0", "configurations": [ { "name": "Python: 現在のファイル", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal", "justMyCode": false, "args" : [ "python", "selenium", ] } ] }
launch.json { "version": "0.2.0", "configurations": [ { "name": "Python: 現在のファイル", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal", "justMyCode": false, "args" : [ "python", "selenium", ] } ] }
6.結果
6.結果
下記のような文言がVScodeのターミナルに出てくれば、正常に動作しています。
下記のような文言がVScodeのターミナルに出てくれば、正常に動作しています。
<html itemscope="" itemtype="http://schema.org/SearchResultsPage" lang="ja"><head><meta cha(後略)
<html itemscope="" itemtype="http://schema.org/SearchResultsPage" lang="ja"><head><meta cha(後略)
おわりに
おわりに
バージョンが変わったことで、既存のウェブサイトに書かれている通りにやってみたけどなんか動かないな、という方の参考になれば幸いです。
バージョンが変わったことで、既存のウェブサイトに書かれている通りにやってみたけどなんか動かないな、という方の参考になれば幸いです。

ウェブスクレイピングの始め方

GoogleseleniumprintThis article provides a stepbystep guide on how to get started with web scraping using Python. The guide covers installing the necessary software, creating a virtual environment, installing Selenium, downloading the Chrome driver, and writing a Python script to search Google for the term python selenium.
by JanitorJun 18, 2023
情報収集や事務作業、発注作業の自動化などを目的としてウェブスクレイピングを始めようとされている方に向けた情報です。 バージョンが変わったりなどして始めにくいような状況かと思いますが、最初の一歩として、Googleで検索をその結果をprintします。 環境は、Windows11、Chrome、Visual Studio Code、pythonです。
情報収集や事務作業、発注作業の自動化などを目的としてウェブスクレイピングを始めようとされている方に向けた情報です。 バージョンが変わったりなどして始めにくいような状況かと思いますが、最初の一歩として、Googleで検索をその結果をprintします。 環境は、Windows11、Chrome、Visual Studio Code、pythonです。

Jump Links

1.仮想環境作成用のvirtualenvをインストール
2.仮想環境作成
3.仮想環境に入ってseleniumをインストール
4.Chromeドライバーをダウンロード
5.Pythonファイルを作成
6.結果
おわりに
1.仮想環境作成用のvirtualenvをインストール
1.仮想環境作成用のvirtualenvをインストール
コマンドプロンプトで下記コマンドを実行します。
コマンドプロンプトで下記コマンドを実行します。
pip install virtualenv
pip install virtualenv
2.仮想環境作成
2.仮想環境作成
仮想環境作成のためのコマンドを入力します。 環境名は、「testEnv」としています。
仮想環境作成のためのコマンドを入力します。 環境名は、「testEnv」としています。
python -m venv testEnv
python -m venv testEnv
3.仮想環境に入ってseleniumをインストール
3.仮想環境に入ってseleniumをインストール
仮想環境を作成したフォルダで下記コマンドを実行します。 seleniumのインストールされたバージョンは、「4.10.0」でした。 最初のコマンドが作成した仮想環境に入るコマンドです。 出るときは、「deactivate」を実行すると出られます。
仮想環境を作成したフォルダで下記コマンドを実行します。 seleniumのインストールされたバージョンは、「4.10.0」でした。 最初のコマンドが作成した仮想環境に入るコマンドです。 出るときは、「deactivate」を実行すると出られます。
testEnv\Scripts\activate pip install selenium pip install selenium-wire pip list (中略) selenium 4.10.0 (後略)
testEnv\Scripts\activate pip install selenium pip install selenium-wire pip list (中略) selenium 4.10.0 (後略)
4.Chromeドライバーをダウンロード
4.Chromeドライバーをダウンロード
下記のURLからドライバーをダウンロードして解凍します。 Windowsの場合は、「chromedriver_win32.zip」を選択してください。 解凍すると中身は、「chromedriver.exe」というexeで、好きなフォルダにおいてください。 あとで、その場所をpythonファイルに書きます。 https://chromedriver.chromium.org/downloads
下記のURLからドライバーをダウンロードして解凍します。 Windowsの場合は、「chromedriver_win32.zip」を選択してください。 解凍すると中身は、「chromedriver.exe」というexeで、好きなフォルダにおいてください。 あとで、その場所をpythonファイルに書きます。 https://chromedriver.chromium.org/downloads
5.Pythonファイルを作成
5.Pythonファイルを作成
pythonファイル、launch.jsonにそれぞれ下記のように書き込みます。 実行時の引数は「python selenium」としていて、seleniumを使って、「python selenium」をgoogleで検索させています。
pythonファイル、launch.jsonにそれぞれ下記のように書き込みます。 実行時の引数は「python selenium」としていて、seleniumを使って、「python selenium」をgoogleで検索させています。
pythonファイル import sys from seleniumwire import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.proxy import Proxy, ProxyType from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.keys import Keys googleUrl = "https://google.com/search?q=" searchWords = [] def main(): service = Service(executable_path='chromedriver.exeへのパス') driver = webdriver.Chrome(service=service) searchWordsStr = "" if( len( searchWords ) > 0 ): searchWordsStr = '+'.join( searchWords[ 1 : ] ) else: return driver.implicitly_wait( 10 ) driver.get( 'https://google.com/search?q=' + searchWordsStr ) print( driver.page_source ) if __name__ == '__main__': searchWords = sys.argv main()
pythonファイル import sys from seleniumwire import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.proxy import Proxy, ProxyType from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.keys import Keys googleUrl = "https://google.com/search?q=" searchWords = [] def main(): service = Service(executable_path='chromedriver.exeへのパス') driver = webdriver.Chrome(service=service) searchWordsStr = "" if( len( searchWords ) > 0 ): searchWordsStr = '+'.join( searchWords[ 1 : ] ) else: return driver.implicitly_wait( 10 ) driver.get( 'https://google.com/search?q=' + searchWordsStr ) print( driver.page_source ) if __name__ == '__main__': searchWords = sys.argv main()
launch.json { "version": "0.2.0", "configurations": [ { "name": "Python: 現在のファイル", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal", "justMyCode": false, "args" : [ "python", "selenium", ] } ] }
launch.json { "version": "0.2.0", "configurations": [ { "name": "Python: 現在のファイル", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal", "justMyCode": false, "args" : [ "python", "selenium", ] } ] }
6.結果
6.結果
下記のような文言がVScodeのターミナルに出てくれば、正常に動作しています。
下記のような文言がVScodeのターミナルに出てくれば、正常に動作しています。
<html itemscope="" itemtype="http://schema.org/SearchResultsPage" lang="ja"><head><meta cha(後略)
<html itemscope="" itemtype="http://schema.org/SearchResultsPage" lang="ja"><head><meta cha(後略)
おわりに
おわりに
バージョンが変わったことで、既存のウェブサイトに書かれている通りにやってみたけどなんか動かないな、という方の参考になれば幸いです。
バージョンが変わったことで、既存のウェブサイトに書かれている通りにやってみたけどなんか動かないな、という方の参考になれば幸いです。
© 2023 - Comytom LLC