python3 + selenium4 + chromedriver.exe v101 による edgeブラウザの自動化 - end0tknr's kipple - web写経開発
以下、上記entry の firefox版です。
#!python3 # -*- coding: utf-8 -*- from selenium import webdriver # ex. pip install selenium==4.1.3 from selenium.webdriver.firefox.options import Options from selenium.webdriver.firefox.service import Service from selenium.webdriver.common.by import By import os import re import sys import time browser_conf = { "browser_driver" : os.getcwd() + '\\geckodriver.exe', "browser_options" : [ #"--headless", ], "implicitly_wait": 10 } pref_names = [ # "saitama","chiba", "tokyo", # "kanagawa", ] base_urls = [ "https://www.homes.co.jp/kodate/shinchiku/%s/", #新築戸建 ] def main(): for base_url in base_urls: browser = init_browser() for pref_name in pref_names: search_result_urls = \ get_search_result_urls(browser, base_url, pref_name) time.sleep(10) # for search_result_url in search_result_urls: # print("\t".join([base_url,pref_name,search_result_url])) browser.close() def get_search_result_urls(browser, base_url, pref_name): func_name = sys._getframe().f_code.co_name print(func_name, base_url, pref_name, file=sys.stderr) req_url = base_url % ( pref_name ) browser.get( req_url ) label_elms = browser.find_elements(by=By.CSS_SELECTOR, value="label") re_compile_cityall = re.compile("cond\[city\]\[all\]") for label_elm in label_elms: atri_for = str( label_elm.get_attribute("for") ) if not re_compile_cityall.search(atri_for): continue label_elm.click() btn_elms = browser.find_elements(by=By.CSS_SELECTOR, value="button") for btn_elm in btn_elms: atri_type = str( btn_elm.get_attribute("type") ) print( atri_type ) if str( atri_type ) != "submit": continue time.sleep(5) btn_elm.click() def init_browser(): browser_service = Service( executable_path=browser_conf["browser_driver"] ) browser_opts = Options() for tmp_opt in browser_conf["browser_options"]: browser_opts.add_argument( tmp_opt ) browser = webdriver.Edge(service = browser_service, options = browser_opts ) # 要素が見つかるまで、最大 ?秒 待つ browser.implicitly_wait( browser_conf["implicitly_wait"] ) return browser if __name__ == '__main__': main()