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?

Excel csvに保存

Last updated at Posted at 2025-07-08

自動検索 : たくさんリンクとたくさんキーワード

リンク : json⭕️  CSV

キーワード : CSV⭕️  json

code

import pandas as pd 
import json
import webbrowser
import time
import os
import csv
from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.service import Service

# code ここから
keywords_df = pd.read_excel("求人.xlsx", sheet_name="シート1", header=None)

# Excelの1行目をキーワードとしてリスト化
keywords = keywords_df.fillna('').values.flatten().tolist()

results = []

# Firefoxのドライバ設定
options = Options()
options.headless = False  # GUIモードで開く(headless=TrueにするとGUIなしで動作)

geckodriver_path = '/usr/local/bin/geckodriver'
service = Service(geckodriver_path)
driver = webdriver.Firefox(service=service, options=options)

base_url = 'https://duckduckgo.com/?q='
driver.get(base_url)

# 現在のタブ(最初のタブ)を記録
original_window = driver.current_window_handle

# 各キーワードを使って検索
for keyword in keywords:
    driver.execute_script("window.open('');")  # 新しいタブを開く
    driver.switch_to.window(driver.window_handles[-1])  # 新しいタブに切り替え
    driver.get("https://duckduckgo.com/")  # 検索ページを開く
    time.sleep(1)  # ロード待ち

    search_box = driver.find_element(By.NAME, "q")
    search_box.clear()
    search_box.send_keys(keyword)
    search_box.send_keys(Keys.RETURN)
    time.sleep(1)  # 検索結果のロード待ち

# すべてのタブを順番に処理
for i in range(1, len(driver.window_handles)):  # 1からスタート(最初のタブは除外)
    driver.switch_to.window(driver.window_handles[i])  # 各タブに切り替え
    time.sleep(1)  # 読み込み待機

    # BeautifulSoupでページ解析
    soup = BeautifulSoup(driver.page_source, "html.parser")
    links = soup.find_all("a", href=True)

    # URLを取得してリストに追加
    for link in links:
        url = link["href"]
        if "http" in url:
            results.append({"keyword": keywords[i - 1], "url": url})  # キーワード対応


# 結果をCSVに保存
output_file = "search_results.csv"
pd.DataFrame(results).to_csv(output_file, index=False, encoding="utf-8")
print(f"検索結果を {output_file} に保存しました。")


いるもん

pip install openpyxl selenium beautifulsoup4 pandas

解説

タブを切り替えて表示の処理 全部
original_window = driver.current_window_handle

# させたい処理 start
# させたい処理 finish

for i in range(1, len(driver.window_handles)):  # 1からスタート(最初のタブは除外)
    driver.switch_to.window(driver.window_handles[i])  # 各タブに切り替え
    time.sleep(1)  # 読み込み待機

    # BeautifulSoupでページ解析
    soup = BeautifulSoup(driver.page_source, "html.parser")
    links = soup.find_all("a", href=True)

    for link in links:
        url = link["href"]
        if "http" in url:
            results.append({"keyword": keywords[i - 1], "url": url})  # キーワード対応


キーワード検索
for keyword in keywords:
    driver.execute_script("window.open('');")  # 新しいタブを開く
    driver.switch_to.window(driver.window_handles[-1])  # 新しいタブに切り替え
    driver.get("https://duckduckgo.com/")  # 検索ページを開く
    time.sleep(1)  # ロード待ち

    search_box = driver.find_element(By.NAME, "q")
    search_box.clear()
    search_box.send_keys(keyword)
    search_box.send_keys(Keys.RETURN)
    time.sleep(1)

excelデータ読み込みとcsvデータ保存
keywords_df = pd.read_excel("求人.xlsx", sheet_name="シート1", header=None)

# Excelの1行目をキーワードとしてリスト化
keywords = keywords_df.fillna('').values.flatten().tolist()

results = []

# 他の処理 start
# 他の処理 finish


output_file = "search_results.csv"
pd.DataFrame(results).to_csv(output_file, index=False, encoding="utf-8")
print(f"検索結果を {output_file} に保存しました。")

キーワードに、中途採用を付け検索
# ここで "中途採用" をキーワードに追加
keywords = ["中途採用"]  # 他にも検索したいキーワードを追加可能

for keyword in keywords:
    driver.execute_script("window.open('');")  # 新しいタブを開く
    driver.switch_to.window(driver.window_handles[-1])  # 新しいタブに切り替え
    driver.get("https://duckduckgo.com/")  # 検索ページを開く
    time.sleep(1)  # ロード待ち

    search_box = driver.find_element(By.NAME, "q")
    search_box.clear()
    search_box.send_keys(keyword)  # キーワードを入力
    search_box.send_keys(Keys.RETURN)  # 検索実行
    time.sleep(1)  # 結果が表示されるまで待機


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?