curl取得、解析、lynx抽出awkに使用されます。

curl取得、解析、lynx抽出awkに使用されます。

これを考慮すると:

<p>Currencies fluctuate every day. The rate shown is effective for transactions submitted to Visa on <strong>February 5, 2017</strong>, with a bank foreign transaction fee of <st <span><strong>1</strong> Euro = <strong>1.079992</strong> United States Dolla <p>The 'currency calculator' below gives you an indication of the cost of purchas <p>February 5, 2017</p><div class="clear-both"></div> <!-- removed clearboth- <p><strong>1 EUR = 1.079992 USD</strong></p> <div class="clear-both"></di <table width="290" border="0" cellspacing="0" cellpadding="3"> <a href="/content/VISA/US/en_us/home/support/consumer/travel-support/exchange e-calculator.html"> <button class="btn btn-default btn-xs"><span class="retur <p><p>This converter uses a single rate per day with respect to any two currencies. Rates displayed may not precisely reflect actual rate applied to transaction amount due to rounding differences, Rates apply to the date the transaction was processed by Visa; this may differ from the actual date of the transaction. Banks may or may not assess foreign transaction fees on cross-border transactions. Fees are applied at banks’ discretion. Please contact your bank for more information.</p>

抽出する必要があります1.079992

私は以下を使用しています:

sed -E 's:.*(1\.[0-9\.]+).*:\1:g

...これはうまくいきます...しかし、よりエレガントな方法はありますか?

または直接値を取得する方法はありますかcurl

(私の完全なコマンドは次のとおりですcurl 'https://usa.visa.com/support/consumer/travel-support/exchange-rate-calculator.html/?fromCurr=USD&toCurr=EUR&fee=0&exchangedate=02/05/2017' | grep '<p><strong>1' | sed -E 's:.*(1\.[0-9\\.]+).*:\1:g' :)

ベストアンサー1

curl取得、解析、lynx抽出awkに使用されます。

を使用しないでくださいsedgrepXML/HTML の構文解析を待ってください。 HTMLはコンテキストフリーですが、sed友達はただ普通です。1

url='https://usa.visa.com/support/consumer/travel-support/exchange-rate-calculator.html/?fromCurr=USD&toCurr=EUR&fee=0&exchangedate=02/05/2017'
user_agent= 'Mozilla/5.0 (X11; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0'

curl -sA "${user_agent}" "${url}"  \
| lynx -stdin -dump                \
| awk '/1 EUR/{ print $4 }'

コンテンツを確実に抽出するには、一種のHTMLパーサーが必要です。ここではlynxテキストベースのWebブラウザを使用していますが、より軽い選択肢もあります。

ここでは、curlページが検索され、解析されlynxダンプされます。テキスト表現。文字/1 EUR/列の理由を検索すると、次の行だけが見つかりました。awk1 EUR

   1 EUR = 1.079992 USD

それから{ print $4 }4番目の列です1.079992

代替ソリューションは提供されませんcurl

私が選択したHTMLパーサーは次のとおりですlynxcurl

url='https://usa.visa.com/support/consumer/travel-support/exchange-rate-calculator.html/?fromCurr=USD&toCurr=EUR&fee=0&exchangedate=02/05/2017'
user_agent= 'Mozilla/5.0 (X11; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0'

lynx -useragent="${user_agent}" -dump "${url}"  \
| awk '/1 EUR/{ print $4 }'

1 Aは、pcregrep -P一部の実装では)以下を説明できます。一部コンテキストがない、または状況に合った文字列のセットですが、すべてではありません。


2017-12-23で編集curlサイトが現在ブロックされているので、ユーザーエージェント文字列(Firefoxのチャック)を追加してくださいlynx

おすすめ記事