前提
Pythonは使わず、
GASを使用してログインが必要なWebページのスクレイピングして、
取得した文字列をGoogleスプレッドシートに出力したいです。
実現したいこと
ログイン後のページのhtml内の要素の取得
↓
eBayアカウントページ(下図参考)の
右セクションShipping details内のDate shipped
の値「Aug 11, 2022」を取得したいです。
発生している問題・エラーメッセージ
ログに出力しても想定通りの結果になりませんでした。。
これはページが動的だからでしょうか。
それとも「Security Measure」と表示されるので、
なんらかのセキュリティ対策が取られているからでしょうか。
アドバイスいただけますと幸いです。
よろしくお願いいたします。
該当のソースコード
以下を抽出しています。
①右ブロックの「Shipping details」
②「Date shipped」のタグ
javascript
1 var scraping_url = urlArray[0]; 2 3 response = UrlFetchApp.fetch(scraping_url, get_option); 4 var content = response.getContentText("UTF-8"); 5 6 // ①ブロックを抽出 7 var shipping_details_block = Parser.data(content).from('class="card-old last-card">').to('</div>').build(); 8 Logger.log(shipping_details_block); 9 10 // ②指定のタグで囲まれているテキストを抽出 11 var content_block = Parser.data(content).from('<dt class="info-label">Date shipped</dt><dd>').to('</dd>').iterate(); 12 Logger.log(content_block);
①抽出したブロックのログ
Logger.log(shipping_details_block);
1en><head><meta http-equiv=X-UA-Compatible content=IE=Edge><meta name=viewport content="width=device-width, initial-scale=1"><link rel=icon href=https://pages.ebay.com/favicon.ico><link rel="dns-prefetch" href="//ir.ebaystatic.com"><link rel="dns-prefetch" href="//secureir.ebaystatic.com"><link rel="dns-prefetch" href="//i.ebayimg.com"><link rel="dns-prefetch" href="//rover.ebay.com"><script>$ssgST=new Date().getTime();</script><meta charset=utf-8><title>Security Measure</title><style> 2 #dCF_captcha_text { 3 display: none !important; 4 } 5 .pgHeading { 6 margin: 10px 0px 20px 10px; 7 padding: 10px 0px 0px 0px; 8 } 9 .pgHeading > h1 { 10 line-height: 27px; 11 text-align: left; 12 margin: 0px 10px 20px 0px; 13 font-size: 1.2em; 14 font-family: Arial, Helvetica, sans-serif; 15 font-weight: normal; 16 color: #5d5d5d; 17 } 18 .pgCenter { 19 margin: 10px 10px 10px 10px; 20 text-align: justify; 21 font-size: 1.1em; 22 } 23 .captcha-not-rendered-msg-div a:link { 24 text-decoration: underline; 25 } 26 </style><link rel="stylesheet" type="text/css" href="https://ir.ebaystatic.com/rs/v/ggfnycmu2m0o5nosyro3abui0ma.css?proc=DU:N"><link rel=stylesheet href=https://ir.ebaystatic.com/rs/c/captcha-CpAsu5pR.css></head><body class=no-touch><div id=gh-gb tabindex=-1>
②タグ取得時のログ
Logger.log(content_block);
1[<!DOCTYPE html><html lang=en><head><meta ht]
試したこと
matchメソッドでテキストを抜き出せるのでは。
と思い試してみましたが、セキュリティ対策によりできず。
javascript
1 var regText = /<title>([\s\S]*?)<\/title>/; 2 var html = content.match(regText); 3 Logger.log(html);
ログ
Logger.log(html);
1[<title>Security Measure</title>, Security Measure]
補足情報(FW/ツールのバージョンなど)
参考サイト:
GASでログインしてスクレイピングする方法【コード付きで解説】
GASでWebスクレイピングを行う方法
【GAS】matchメソッドの使い方・正規表現
回答1件
あなたの回答
tips
プレビュー