実現したいこと
Google MapをPythonのBeautiful soupとseleniumでスクレイピングしたいです。(初級者ですが、Beautifulsoupを使って2~3回、スプレイピングに成功したことがあります。)
前提
今回は、Google Mapから企業リストを取得するために、Beautiful soupとseleniumを使ってプログラムを組もうとしていますが、データが抜き取れません。
参考にしたサイトは、https://tinyurl.com/2phl2tme です。
該当のソースコード
python3
1pip install selenium 2pip install parsel 3from selenium import webdriver 4pip install --upgrade requests 5pip install chromedriver_binary==111.0.5563.64 6import chromedriver_binary 7import requests 8from time import sleep 9from selenium.webdriver.chrome.options import Options 10from bs4 import BeautifulSoup 11webdriver.Chrome() 12driver = webdriver.Chrome() 13url = 'https://www.google.com/maps?q=%E5%8D%83%E8%91%89%E5%B8%82+%E5%BB%BA%E8%A8%AD%E4%BC%9A%E7%A4%BE&hl=ja&iflsig=AOEireoAAAAAZCat3XNUJkImg4kSoVb1a1ubqqKJkemR&uact=5&gs_lcp=Cgdnd3Mtd2l6EAMyBQgAEIAEMgUIABCABDIGCAAQHhAPMgsIABAIEB4QDxDxBDIGCAAQCBAeOggIABCPARDqAlDgAVjgAWDTBmgBcAB4AIAB9AGIAfQBkgEDMi0xmAEAoAECoAEBsAEK&um=1&ie=UTF-8&sa=X&ved=2ahUKEwi0u5Pg5YX-AhWgk1YBHVw9DWUQ_AUoAXoECAEQAw' 14driver.get(url) 15page_content = driver.page_source 16from parsel import Selector 17response = Selector(page_content) 18results = [] 19# ここは丸写しなので、よく理解していません。 20for el in response.xpath('//div[contains(@aria-label, "Results for")]/div/div[./a]'): 21 results.append({ 22 'link': el.xpath('./a/@href').extract_first(''), 23 'title': el.xpath('./a/@aria-label').extract_first('') 24 }) 25 26print(results) 27driver.quit() 28
試したこと
for el in response.xpath 以下のところが間違っているかもしれません。(自分では理解不能ですが)
もしくは、Chromeの検証からソースを見ていますが、住所や会社名などの必要なデータがscriptに入っているので、そこを読み取ることができないのかもしれません。
<script nonce="">(function(){if(window.tactilecsi){window.tactilecsi.g={};window.tactilecsi.h=1;window.tactilecsi.setTimerName=function(d,a){d.name=a};var n=function(d,a,g){var b="";window.tactilecsi.srt&&(b+="&srt="+window.tactilecsi.srt,delete window.tactilecsi.srt);window.tactilecsi.pt&&(b+="&tbsrt="+window.tactilecsi.pt,delete window.tactilecsi.pt);try{window.external&&window.external.tran?b+="&tran="+window.external.tran:window.gtbExternal&&window.gtbExternal.tran?b+="&tran="+window.gtbExternal.tran():window.chrome&&window.chrome.csi&& (b+="&tran="+window.chrome.csi().tran)}catch(q){}var c=window.chrome;if(c&&(c=c.loadTimes)){c().wasFetchedViaSpdy&&(b+="&p=s");if(c().wasNpnNegotiated){b+="&npn=1";var e=c().npnNegotiatedProtocol;e&&(b+="&npnv="+(encodeURIComponent||escape)(e))}c().wasAlternateProtocolAvailable&&(b+="&apa=1")}if("undefined"!=typeof navigator&&navigator&&navigator.connection){c=navigator.connection;e=c.type;for(var f in c)if("type"!=f&&c[f]==e){b+="&conn="+f;break}}c=d.t;e=c.start;f=[];for(var h in c)if("start"!=h&& e){var k=d.t[h];var l=d.t.start;k&&l?(k-=l,k=Math.round(k)):k=void 0;f.push(h+"."+k)}delete c.start;if(a)for(var m in a)b+="&"+m+"="+a[m];(a=g)||(a="https:"==document.location.protocol?"https://csi.gstatic.com/csi":"http://csi.gstatic.com/csi");return d=[a,"?v=3","&s="+(window.tactilecsi.sn||"tactile")+"&action=",d.name,"",b,"&rt=",f.join(",")].join("")};window.tactilecsi.getReportUri=n;var p=function(d,a,g){d=n(d,a,g);if(!d)return"";a=new Image;var b=window.tactilecsi.h++;window.tactilecsi.g[b]= a;a.onload=a.onerror=function(){window.tactilecsi&&delete window.tactilecsi.g[b]};a.src=d;a=null;return d};window.tactilecsi.report=function(d,a,g){var b=document.visibilityState,c="visibilitychange";b||(b=document.webkitVisibilityState,c="webkitvisibilitychange");if("prerender"==b){var e=!1,f=function(){if(!e){a?a.prerender="1":a={prerender:"1"};if("prerender"==(document.visibilityState||document.webkitVisibilityState))var h=!1;else p(d,a,g),h=!0;h&&(e=!0,document.removeEventListener(c,f,!1))}};document.addEventListener(c,f,!1);return""}return p(d,a,g)}};}).call(this);</script>
発生している問題・エラーメッセージ
下記URLを参考にしています。一部ソースをそのまま転用しています。
https://tinyurl.com/2phl2tme
補足情報(FW/ツールのバージョンなど)
Jupyter notebook
あなたの回答
tips
プレビュー