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?

Webサイトをpdfにし、pdfから前株と後ろ株の会社名検索、検索dataをCSVに保存

Posted at

いるもん

code

☺️
import pdfplumber
import pandas as pd
import re

# PDFファイルからテキストを抽出
def extract_text_from_pdf(pdf_path):
    text = ""
    with pdfplumber.open(pdf_path) as pdf:
        for page in pdf.pages:
            text += page.extract_text()  # ページからテキストを抽出
    return text


# 「株式会社」を含む会社名を抽出
def extract_companies(text):
    pattern = r"株式会社[\w一-龯々〆〤]+|[\w一-龯々〆〤]+株式会社"  
    # 前株と後かぶ を取得
    companies = re.findall(pattern, text)
    return list(set(companies))

# 使いたいPDF
pdf_paths = [
    "/Users/chomechomesuke/Desktop/indeed.pdf",
    "/Users/chomechomesuke/Desktop/indeed2.pdf",
]

results = []

# PDFから抽出したデータをcsvに保存
for pdf_path in pdf_paths:
    text = extract_text_from_pdf(pdf_path)
    if not text:
        print(f" No text  {pdf_path}")
    else:
        print(f" {text[:300]}...")  
        # 最初の300文字を表示

    companies = extract_companies(text)
    print(f"Data: {companies}")

    for company in companies:
        results.append({"company_name": company})

# 結果をCSVに保存
if results:
    output_file = "search_results.csv"
    df = pd.DataFrame(results)
    df.to_csv(output_file, index=False, encoding="utf-8")
    print(f"保存成功")
else:
    print("めよら")

# CSVから読み込む
df = pd.read_csv(output_file)

# 部分一致の抽出(正規表現も使用可能)
partial_match = df[df['company_name'].str.contains('検索したいキーワード', na=False)]

print(partial_match)

解説

1️⃣ PDFからテキスト取り出す
def extract_text_from_pdf関数 定義

最初に、から配列用意
text = ""
PDFファイル開けて
with pdfplumber.open(pdf_path) as pdf:
PDFから、テキスト取り出す
for page in pdf.pages:
    text += page.extract_text()
return text

2️⃣ PDFから、前株と後かぶ 取得
def extract_companies関数 定義

PDFから、テキスト取り出す
    # 前株と後かぶ を取得
    pattern = r"株式会社[\w一-龯々〆〤]+|[\w一-龯々〆〤]+株式会社"  
    
    companies = re.findall(pattern, text)
    
    return list(set(companies))

3️⃣ 定義した変数を呼び出し、データを出力 & CSV保存
pdf_path in pdf_paths:関数

定義した変数呼び出し、指定条件でデータ出力
    text = extract_text_from_pdf(pdf_path)

    companies = extract_companies(text)

    for company in companies:
        results.append({"company_name": company})
pandaでcsv 保存
    output_file = "search_results.csv"
    
    df = pd.DataFrame(results)
    
    df.to_csv(output_file, index=False, encoding="utf-8")

デバッグ

1️⃣データcsv保存⭕️ : "成功"、
保存❌ : "めよら"と表示

if results:
    output_file = "search_results.csv"
    df = pd.DataFrame(results)
    df.to_csv(output_file, index=False, encoding="utf-8")
    print(f"成功")
else:
    print("めよら")

2️⃣データPDFから取り出し⭕️ : 結果表示
保存❌ : "めよら"と表示

text = extract_text_from_pdf(pdf_path)
    if not text:
        print(f" めよら  {pdf_path}")
    else:
        print(f" {text[:300]}...") 

3️⃣ 前株と後ろ株のデータ取得できてるか、変数の中身確認

companies = extract_companies(text)
    print(f"Data: {companies}")
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?