<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>BeautifulSoup | VBA・GAS・Pythonで仕事を楽しく効率化</title>
	<atom:link href="https://officevba.info/tag/beautifulsoup/feed/" rel="self" type="application/rss+xml" />
	<link>https://officevba.info</link>
	<description>仕事の役に立つVBA・GAS・Pythonのコードを紹介していきます。</description>
	<lastBuildDate>Wed, 10 Jul 2024 12:16:43 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.1</generator>

<image>
	<url>https://officevba.info/wp-content/uploads/2017/04/cropped-Excel_1-32x32.jpg</url>
	<title>BeautifulSoup | VBA・GAS・Pythonで仕事を楽しく効率化</title>
	<link>https://officevba.info</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Yahoo乗り換え検索を用いて所要時間確認（Pythonバージョン）</title>
		<link>https://officevba.info/norikaepython/</link>
					<comments>https://officevba.info/norikaepython/#respond</comments>
		
		<dc:creator><![CDATA[okumasahito]]></dc:creator>
		<pubDate>Mon, 22 Jan 2024 14:09:09 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[BeautifulSoup]]></category>
		<category><![CDATA[Selenium]]></category>
		<category><![CDATA[スクレイピング]]></category>
		<category><![CDATA[乗り換え]]></category>
		<guid isPermaLink="false">https://officevba.info/?p=2488</guid>

					<description><![CDATA[目次 IEが動かなくなってもスクレイピングをしたいモジュールのインストール用意する条件読み込み用・出力用のExcelファイル作成したPythonのコード IEが動かなくなってもスクレイピングをしたい 私はよくVBAを使っ [&#8230;]]]></description>
										<content:encoded><![CDATA[
  <div id="toc" class="toc tnt-number toc-center tnt-number border-element"><input type="checkbox" class="toc-checkbox" id="toc-checkbox-2" checked><label class="toc-title" for="toc-checkbox-2">目次</label>
    <div class="toc-content">
    <ol class="toc-list open"><li><a href="#toc1" tabindex="0">IEが動かなくなってもスクレイピングをしたい</a></li><li><a href="#toc2" tabindex="0">モジュールのインストール</a></li><li><a href="#toc3" tabindex="0">用意する条件読み込み用・出力用のExcelファイル</a></li><li><a href="#toc4" tabindex="0">作成したPythonのコード</a></li></ol>
    </div>
  </div>

<h2><span id="toc1">IEが動かなくなってもスクレイピングをしたい</span></h2>
<p>私はよくVBAを使ってIEを操作するスクレイピングを好んで使っていたのですが、IEのサポートが終了して使えない機能が出てきました。<br />
（開いている画面を取得して操作できたりなどPythonのBeautifulSoup・SeleniumやGASではできないこともあったので便利だったのですが仕方ないですね…。）</p>
<p>以前に<a href="https://officevba.info/norikaevba2/" target="_blank">こちら</a>でご紹介した乗り換え検索・所要時間算出ツールもIEをベースに作っていて、画面遷移がないので使えなくはないみたいでしたがアラート表示が出て継続使用に不安が出てきましたので代替のツールを作ってみました。</p>
<p>（ひょっとすると他の乗り換え検索もですが、）Yahooの乗り換え検索は駅・バス停間の所要時間だけでなく、住所を入力すれば住所地からの徒歩の時間も算出してくれるので、おおよその所要時間を把握するのにとても便利です。</p>
<p>今回はPythonのBeautifulSoupを使用して出発地・到着地間の所要時間を算出するコードを書いてみました。</p>
<h2><span id="toc2">モジュールのインストール</span></h2>
<p>今回はBeautifulSoupとopenpyxlを使用するので、あらかじめ以下のモジュールをインストールしておきます。</p>
<div class="PythonCode">
<pre>
rem コマンドプロンプト
pip install beautifulsoup4
pip install openpyxl</pre>
</div>
<h2><span id="toc3">用意する条件読み込み用・出力用のExcelファイル</span></h2>
<p>検索条件を入力したり、取得した情報を書き出すためにExcelのファイルを使用します。<br />
書式は以下のようなものを準備していて、A～L列（背景色なし）に所要時間を調べたい条件を入力してPythonのコードを実行するとM～S列（背景色水色）の欄に調べた情報が出力されるものになります。</p>
<p><a href="https://officevba.info/wp-content/uploads/2024/01/vba171_1.png"><img fetchpriority="high" decoding="async" src="https://officevba.info/wp-content/uploads/2024/01/vba171_1-700x131.png" alt="" width="700" height="131" class="alignnone size-large wp-image-2486" srcset="https://officevba.info/wp-content/uploads/2024/01/vba171_1-700x131.png 700w, https://officevba.info/wp-content/uploads/2024/01/vba171_1-300x56.png 300w, https://officevba.info/wp-content/uploads/2024/01/vba171_1-768x144.png 768w, https://officevba.info/wp-content/uploads/2024/01/vba171_1-1536x288.png 1536w, https://officevba.info/wp-content/uploads/2024/01/vba171_1-2048x384.png 2048w" sizes="(max-width: 700px) 100vw, 700px" /></a></p>
<h2><span id="toc4">作成したPythonのコード</span></h2>
<div class="PythonCode">
<pre>
# Python
from bs4 import BeautifulSoup

import os
import datetime
from enum import IntEnum

import glob
import openpyxl
import urllib.parse

import requests

t_delta = datetime.timedelta(hours=9)
JST = datetime.timezone(t_delta, 'JST')
now = datetime.datetime.now(JST)
print(now.strftime('%Y%m%d_%H%M%S'))

def getRoute():
    for path in glob.glob(os.getcwd() + r'\py乗り換え検索.xlsx'):
        lwb = openpyxl.load_workbook(filename=path)
        lsh = lwb.worksheets[0]#シート1を選択

        tarRow = 2
        while not lsh.cell(tarRow,1).value is None: #3スタートでセルの値がなくなるまで繰り返す
            print(getUrl(lsh, tarRow))
            response = requests.get(getUrl(lsh, tarRow))
            response.encoding = response.apparent_encoding
            bs = BeautifulSoup(response.text, 'html.parser')
            lsh.cell(tarRow,  int(col.条件入力)).value = bs.select("span.time")[0].text
            lsh.cell(tarRow,  int(col.時間1)).value = bs.select("li.time")[0].text
            lsh.cell(tarRow,  int(col.料金1)).value = bs.select("li.fare")[0].text
            lsh.cell(tarRow,  int(col.時間2)).value = bs.select("li.time")[1].text
            lsh.cell(tarRow,  int(col.料金2)).value = bs.select("li.fare")[1].text
            lsh.cell(tarRow,  int(col.時間3)).value = bs.select("li.time")[2].text
            lsh.cell(tarRow,  int(col.料金3)).value = bs.select("li.fare")[2].text

            tarRow += 1
        lwb.save(path)
        lwb.close

class col(IntEnum):

    出発 = 1
    到着 = 2
    出発到着条件 = 3
    日付 = 4
    時間 = 5
    飛行機 = 6
    新幹線 = 7
    有料特急 = 8
    高速バス = 9
    路線バス = 10
    フェリー = 11
    優先 = 12
    条件入力 = 13
    時間1 = 14
    料金1 = 15
    時間2 = 16
    料金2 = 17
    時間3 = 18
    料金3 = 19

def getUrl(lsh, tarRow):
    
    # return lsh.cell(tarRow,1).value
    出発 = lsh.cell(tarRow,  int(col.出発)).value
    到着 = lsh.cell(tarRow,  int(col.到着)).value

    if lsh.cell(tarRow,  int(col.出発到着条件)).value == "出発":
        検索タイプ = "1"
    elif lsh.cell(tarRow,  int(col.出発到着条件)).value == "到着":
        検索タイプ = "4"
    elif lsh.cell(tarRow,  int(col.出発到着条件)).value == "始発":
        検索タイプ = "3"
    elif lsh.cell(tarRow,  int(col.出発到着条件)).value ==  "終電":
        検索タイプ = "2"
    elif lsh.cell(tarRow,  int(col.出発到着条件)).value ==  "指定なし":
        検索タイプ = "5"
    else:
        検索タイプ = "5"

    if lsh.cell(tarRow,  int(col.日付)) != "" :
        日付 = lsh.cell(tarRow,  int(col.日付)).value
    else:
        日付 = now.strftime('%Y%m%d')
    
    if lsh.cell(tarRow,  int(col.時間)) != "" :
        時間 = lsh.cell(tarRow,  int(col.時間)).value
    else:
        時間 = "9:00"
   
    if lsh.cell(tarRow,  int(col.飛行機)) != "" :
        飛行機 = "1"
    else:
        飛行機 = "0"
   
    if lsh.cell(tarRow,  int(col.新幹線))!= "" :
        新幹線 = "1"
    else:
        新幹線 = "0"
    
    if lsh.cell(tarRow,  int(col.有料特急)) != "" :
        有料特急 = "1"
    else:
        有料特急 = "0"
   
    if lsh.cell(tarRow,  int(col.高速バス)) != "" :
        高速バス = "1"
    else:
        高速バス = "0"
    
    if lsh.cell(tarRow,  int(col.路線バス)) != "" :
        路線バス = "1"
    else:
        路線バス = "0"
   
    if lsh.cell(tarRow,  int(col.フェリー)) != "" :
        フェリー = "1"
    else:
        フェリー = "0"

    if lsh.cell(tarRow,  int(col.優先)) == "到着が早い順":
        検索結果の表示順 = "0"
    elif lsh.cell(tarRow,  int(col.優先)) == "料金が安い順":
        検索結果の表示順 = "1"
    elif lsh.cell(tarRow,  int(col.優先)) == "乗り換え回数順":
        検索結果の表示順 = "2"
    else:
        検索結果の表示順 = "0"

    tarUrl = ("https://transit.yahoo.co.jp/search/result?flatlon=&#038;" + 
    "from=" + urllib.parse.quote(出発) +
    "&#038;tlatlon=" +
    "&#038;to=" + urllib.parse.quote(到着) +
    "&#038;via=&#038;via=&#038;via=" +
    "&#038;y=" + 日付.strftime('%Y') +
    "&#038;m=" + 日付.strftime('%m') +
    "&#038;d=" + 日付.strftime('%d') +
    "&#038;hh=" + 時間.strftime('%H') +
    "&#038;m2=" + 時間.strftime('%M')[1:] +
    "&#038;m1=" + 時間.strftime('%M')[:1] +
    "&#038;type=" + 検索タイプ +
    "&#038;ticket=" + "ic" +
    "&#038;al=" + 飛行機 +
    "&#038;shin=" + 新幹線 +
    "&#038;ex=" + 有料特急 +
    "&#038;hb=" + 高速バス +
    "&#038;lb=" + 路線バス +
    "&#038;sr=" + フェリー +
    "&#038;s=" + 検索結果の表示順 +
    "&#038;expkind=" + "1" + "&#038;ws=" + "3")

    return tarUrl

def sendLineNotify(sendMessage):
    api = "https://notify-api.line.me/api/notify"
    token = "QDSErjbTdCeR5R0mfT7wpKD7HgNQa6MkVyfgLi4vazR"
    headers = {"Authorization" : "Bearer "+ token}

    message = sendMessage
    payload = {"message" :  message}
    post = requests.post(api, headers = headers, params=payload)

def sendGoogleChat(sendMessage):

    webhookUrl = "https://chat.googleapis.com/v1/spaces/AAAAtxHBaG8/messages?key=AIzaSyDdI0hCZtE6vySjMm-WEfRq3CPzqKqqsHI&#038;token=vwSGCEHFEuWS5ffnp3GXWDH8lPG19VKQQOij1feZyzo"
    requests.post(webhookUrl, json={'text': sendMessage})

getRoute()
</pre>
</div>
<p>こちらのプログラムを実行すると、同じフォルダに格納されているExcelの検索条件を元に、Yahoo乗り換え案内のページを開き、所要時間と交通費を抽出します。<br />
あまり多い件数を試していないので推測ですが1,000件くらいは問題なく出力できると思います。</p>
<p>Yahoo乗り換え案内で入力するパラメータはすべてURLに記載されるGET通信なので、パラメータの記載方法さえわかっていればSeleniumを使わずにBeautifulSoupだけで処理できることがわかりました。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://officevba.info/norikaepython/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
