import json import requests from bs4 import BeautifulSoup def get_title(table, css): # 許可業種 result = [i.get_text(strip=True) for i in table.select(f"tbody > tr{css} > td")] return result def get_data(table, css): result = [] for i in table.select(f"tbody > tr{css} > td"): j = i.get_text(strip=True) n = int(j) if j else 0 result.append(n) return result def get_web(number): url = "http://etsuran.mlit.go.jp/TAKKEN/ksGaiyo.do" headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko" } params = {"CMD": "", "sv_licenseNo": number, "caller": "KS"} with requests.Session() as s: r = s.post(url, headers=headers, params=params) if r.status_code == requests.codes.ok: soup = BeautifulSoup(r.content, "html5lib") result = {} # 会社情報 tds = [ t for td in soup.select("table.re_summ > tbody > tr > td") for t in td.stripped_strings ] # 許可番号 result["auth"], result["lic_num"] = tds[0].split(None, 1) # 商号又は名称 result["company"] = tds[2] # 商号又は名称(よみがな) result["company_yomi"] = tds[1] # 代表者名 result["name"] = tds[4] # 代表者名(よみがな) result["name_yomi"] = tds[3] # 郵便番号 result["postal_code"] = tds[5] # 所在地 result["address"] = tds[6] + tds[7] # 電話番号 result["tel"] = tds[8] # 許可業種(全部) tables = soup.select("table.re_summ_3") # 業種名 result["const"] = get_title(tables[0], ".re_summ_ev") # 許可種類 result["license"] = get_data(tables[0], ".re_summ_odd") # パラメーター追加 params["CMD"] = "init" parmit = [] for i in soup.select("table.re_summ_4 > tbody > tr > td > a"): # 許可年月日 date = i.get_text(strip=True) # パラメーター追加 params["licenseDay"] = date # 許可年月日ごとに取得 d_res = s.post(url, headers=headers, params=params) if d_res.status_code == requests.codes.ok: d_soup = BeautifulSoup(d_res.content, "html5lib") # 業種リスト追加 parmit.append( { date: get_data( d_soup.select("table.re_summ_3")[1], ".re_summ_odd" ) } ) # print(parmit) result["license_day"] = parmit # 営業所 if soup.find("img", src="/TAKKEN/images/btn_tab_office_off.png"): params["licenseDay"] = "" o_res = s.post( "https://etsuran.mlit.go.jp/TAKKEN/ksEigyo.do", headers=headers, params=params, ) o_soup = BeautifulSoup(o_res.content, "html5lib") o_list = [] for j in o_soup.select("table.re_office > tbody > tr")[1:]: office = {} o_tds = j.find_all("td", recursive=False) office["id"] = o_tds[0].get_text(strip=True) office["name"], office["tel"] = o_tds[1].stripped_strings office["postal_code"], office["address"] = o_tds[2].stripped_strings office["license"] = get_data( o_tds[3].select_one("table.re_office3"), ":nth-of-type(2)" ) o_list.append(office) result["office"] = o_list # print(result) if result["company"]: filename = result["company"] + ".json" with open(filename, "w") as fw: json.dump(result, fw) if __name__ == "__main__": number = input("許可番号:") # 数字でかつ8文字 if number.isdecimal() and len(number) == 8: get_web(number) else: print("エラー")
PowerShellのExcelベース
if ( $args -eq $null ) { Write-Error '引数がありません' } if (Test-Path $args[0]) { $excel = New-Object -ComObject Excel.Application $excel.Visible = $false try { $book = $excel.Workbooks.Open($args[0]) $sheet = $book.Sheets('Sheet1') # シート数 $book.Sheets.Count # 一番目のシート名 $book.Sheets(1).Name # アクティブシート名 $book.ActiveSheet.Name $sheet.Cells($row, $col) $sheet.Range($sheet.Cells($row, $col), $sheet.Cells($row, $col)) # テキスト(単一セルのみ) $sheet.Range("範囲").Text # 数式(単一セルのみ) $sheet.Range("範囲").Formula # テキスト(単一・複数セル) $sheet.Range("範囲").Value() # 上書き保存 $book.Save() # 別名保存 $book.SaveAs("test.xlsx") # CSV $book.SaveAs("test.csv", 6) # UTF8 CSV $book.SaveAs("test.csv", 62) # Windows CSV $book.SaveAs("test.csv", 23) # Windows テキスト $book.SaveAs("test.csv", 20) # Unicode テキスト $book.SaveAs("test.csv", 42) $book.Close($false) } catch { Write-Error 'エラーが発生しました' } finally { $excel.Quit() $excel = $null [GC]::Collect() } } else { Write-Error 'ファイルが見つかりません' }
CellsかCells.itemどちらが正しいのかわからない
PowerShellでExcelから指定範囲をタブ区切りでクリップボード・TSVへ書き出し
$excel = New-Object -ComObject Excel.Application $excel.Visible = $false $book = $excel.Workbooks.Open($args[0]) $sheet = $book.Sheets('Sheet1') $data = @() # 全範囲 $sheet.UsedRange.Rows | ForEach-Object { $row = @($_.Columns).Text; $data += $row -join "`t" } # 指定範囲 # $sheet.Range("A1:C5").Rows | ForEach-Object { $row = @($_.Columns).Text; $data += $row -join "`t" } # クリップボード $data -join "`n" | Set-Clipboard # TSVで書き出し $data -join "`n" | Out-File -Encoding UTF8 .\outfile.tsv $excel.Quit() $excel = $null [GC]::Collect()
Powershell抽出
# 全体 Get-Content ファイル名 | Select-String -Pattern "検索" | %{$_.Matches.Value} # 全体とマッチ Get-Content ファイル名 | Select-String -Pattern "検索" | %{$_.Matches.Groups.Value} # マッチのみ Get-Content ファイル名 | Select-String -Pattern "検索" | %{$_.Matches.Groups[1].Value}
PowerShellでExcelの指定シートのセルを取得2
範囲の場合はタブ結合
# シート名 $name = 'Sheet1' # 抽出セル $cell = @('A1:D2', 'A1:D1', 'A2', 'B2', 'C2', 'D2') # 抽出データリスト $data = @() # 拡張子変更 $newtext = $args[0] -replace '\.xlsx$', '.txt' $excel = New-Object -ComObject Excel.Application # Excel非表示 $excel.Visible = $false $book = $excel.Workbooks.Open($args[0]) $sheet = $book.Sheets($name) # データ抽出 foreach ($i in $cell) { $row = @() $sheet.Range($i) | ForEach-Object { $row += $_.Text } # タブで結合 $data += $row -join "`t" } # 改行で結合後、ファイルに出力 $data -join "`n" | Out-File -Encoding UTF8 $newtext $book.Close($false) # Excelを閉じる $excel.Quit() # プロセスを解放する $excel = $null [GC]::Collect()