Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
スクレイピングは茨の道/Scraping is a thorny road
Search
cottondesu
December 17, 2018
Programming
0
160
スクレイピングは茨の道/Scraping is a thorny road
前回LT「Python + Selenium + Beautiful Soup でスクレイピング」の
1.おさらい
2.失敗から学ぶ
3.ソースの修正
4.結論
cottondesu
December 17, 2018
Tweet
Share
More Decks by cottondesu
See All by cottondesu
開発環境公開ハード編 / Development environment public hardware version
cottondesu
0
70
開発環境公開ソフト編 / Development environment public software edition
cottondesu
0
65
正規表現で心が折れた/Regular expressions broke my heart
cottondesu
0
80
オレオレGASからMakeへの移行を検討してみた / Considering the transition from Ore Ore GAS to Make
cottondesu
0
410
Kanazawa.rb 10周年KPT / Kanazawa.rb 10th Anniversary KPT
cottondesu
0
540
Kanazawa.rb 9周年KPT / Kanazawa.rb 9th Anniversary KPT
cottondesu
0
350
Mac Book Proのバッテリー交換してみた / I replaced the battery in my Mac Book Pro.
cottondesu
0
460
みんなのCSIRTコミュニティ紹介 / Everyone's CSIRT community introduction
cottondesu
0
360
他人事ではないサイドチャネル攻撃 / Side-channel attacks are no stranger to us.
cottondesu
0
240
Other Decks in Programming
See All in Programming
Realtime API 入門
riofujimon
0
140
Nurturing OpenJDK distribution: Eclipse Temurin Success History and plan
ivargrimstad
0
460
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
500
受け取る人から提供する人になるということ
little_rubyist
0
180
C#/.NETのこれまでのふりかえり
tomokusaba
1
180
Ethereum_.pdf
nekomatu
0
370
役立つログに取り組もう
irof
28
9.4k
Tauriでネイティブアプリを作りたい
tsucchinoko
0
350
Identifying User Idenity
moro
6
9.5k
現場で役立つモデリング 超入門
masuda220
PRO
15
3.1k
「今のプロジェクトいろいろ大変なんですよ、app/services とかもあって……」/After Kaigi on Rails 2024 LT Night
junk0612
4
2k
カスタムしながら理解するGraphQL Connection
yanagii
1
1.5k
Featured
See All Featured
Become a Pro
speakerdeck
PRO
25
5k
Gamification - CAS2011
davidbonilla
80
5k
VelocityConf: Rendering Performance Case Studies
addyosmani
325
24k
Automating Front-end Workflow
addyosmani
1366
200k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.8k
The Language of Interfaces
destraynor
154
24k
Fashionably flexible responsive web design (full day workshop)
malarkey
404
65k
A Tale of Four Properties
chriscoyier
156
23k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.2k
YesSQL, Process and Tooling at Scale
rocio
168
14k
Practical Orchestrator
shlominoach
186
10k
Transcript
2018年12月15日 kanazawa.rb meetup#76 εΫϨΠϐϯά ҵͷಓ
!DPUUPO@EFTV ΏΔͬͱ;ΘͬͱอकΤϯδχΞ ׂ৽ن։ൃɾۀվળ ׂอक ػೳՃɺػೳमਖ਼ɺόάमਖ਼ FUD
ΞδΣϯμ લճ-5ͷ͓͞Β͍ લճ-5ࣦഊ͔ΒֶͿ ιʔεͷमਖ਼ ݁
લճ-5ͷ͓͞Β͍
1ZUIPO 4FMFOJVN #FBVUJGVM4PVQͰεΫϨΠϐϯά
εΫϨΠϐϯάͱʁ
εΫϨΠϐϯά w ΣϒαΠτ͔ΒใΛநग़͢Δ ίϯϐϡʔλιϑτΣΞٕज़ w ݴޠ1FSMɺ1ZUIPOɺ3VCZɺ+BWB4DSJQUͰ ϥΠϒϥϦ͋Γ
શମͷߏਤ
શମͷߏਤ ϑΥϧμ EJSFOW WFOW QZϑΝΠϧ 4FMFOJVN XFCESJWFS ϩάΠϯϖʔδ $ISPNF
%FNP͕՚ྷʹ Τϥʔ
લճ-5ࣦഊ͔ΒֶͿ
ίʔυ֬ೝ
class main(): options = Options() # ChromeͷύεʢStableνϟωϧͰ--headless͕͑ΔΑ͏ʹͳͬͨΒෆཁͳͣʣ options.binary_location = '/Applications/Google
Chrome.app/Contents/MacOS/Google Chrome' # ϔουϨεϞʔυΛ༗ޮʹ͢Δʢ࣍ͷߦΛίϝϯτΞτ͢Δͱը໘͕දࣔ͞ΕΔʣɻ options.add_argument('--headless') # ChromeͷWebDriverΦϒδΣΫτΛ࡞͢Δɻ driver = webdriver.Chrome( os.environ["CHROMEDRIVER"], chrome_options=options) # ϩάΠϯαΠτURL driver.get(os.environ["URL"]) # ϩάΠϯID driver.find_element_by_xpath( "//div[@class='sub-content']/dl/dd/div/input").send_keys( os.environ["LOGIN_USER_ID"]) # ϩάΠϯύεϫʔυ username = driver.find_element_by_xpath( "//div[@class='sub-content']/dl/dd[2]/div/input").send_keys( os.environ["LOGIN_USER_PASS"]) # ϩάΠϯϘλϯԡԼ driver.find_element_by_name("login").click() # ޱ࠲ཧʹભҠ driver.find_element_by_xpath(“//div[@id='link']/ul/li[10]").click() html = driver.page_source soup = BeautifulSoup(html, 'html.parser')
# อ༗ޱɺऔಘ୯Ձɺऔಘ୯ՁɺධՁଛӹͷऔಘ item_name = ['อ༗ޱɹɹ |', 'औಘ୯Ձɹɹ |', 'ج४Ձֹɹɹ |',
'ධՁଛӹɹɹ |'] # ࢿ৴ୗ໊ͷऔಘ investmentname = [] investments = soup.find_all('td', class_='mbody', colspan="3") for investment in investments: investmentname.append(investment.a.text) inves_num = 0 row_num = 0 various_values = soup.find_all('tr', bgcolor='#eaf4e8', align="right") for various_value in various_values: # ࢿ৴ୗ໊ͷදࣔ print(investmentname[inves_num]) # อ༗ޱͷදࣔ print(item_name[row_num], various_value.td.text) for other in various_value.td.find_next_siblings("td"): row_num = row_num + 1 # อ༗ޱҎ֎ͷදࣔ print(item_name[row_num], other.text) row_num = 0 inves_num = inves_num + 1 # શͯͷΫοΩʔΛআ driver.delete_all_cookies() driver.quit() if __name__ == "__main__": main()
ҟৗऴྃ࣌ʹϓϩηε ͕Γଓ͚Δ ϔουϨεϒϥβͷͨΊΘ͔Γʹ͍͘
ղܾࡦ
ҟৗऴྃ࣌Ͱϓϩηε Λऴྃͤ͞Δඞཁ͕͋Δ
ιʔεͷमਖ਼
class main(): try: options = Options() # ChromeͷύεʢStableνϟωϧͰ--headless͕͑ΔΑ͏ʹͳͬͨΒෆཁͳͣʣ options.binary_location =
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome' # ϔουϨεϞʔυΛ༗ޮʹ͢Δʢ࣍ͷߦΛίϝϯτΞτ͢Δͱը໘͕දࣔ͞ΕΔʣɻ options.add_argument('--headless') # ChromeͷWebDriverΦϒδΣΫτΛ࡞͢Δɻ driver = webdriver.Chrome( os.environ["CHROMEDRIVER"], chrome_options=options) # ϩάΠϯαΠτURL driver.get(os.environ["URL"]) # ϩάΠϯID driver.find_element_by_xpath( "//div[@class='sub-content']/dl/dd/div/input").send_keys( os.environ["LOGIN_USER_ID"]) # ϩάΠϯύεϫʔυ username = driver.find_element_by_xpath( "//div[@class='sub-content']/dl/dd[2]/div/input").send_keys( os.environ["LOGIN_USER_PASS"]) # ϩάΠϯϘλϯԡԼ driver.find_element_by_name("login").click() # ޱ࠲ཧʹભҠ driver.find_element_by_xpath(“//div[@id='link']/ul/li[10]").click() html = driver.page_source soup = BeautifulSoup(html, 'html.parser') मਖ਼Օॴ
# อ༗ޱɺऔಘ୯Ձɺऔಘ୯ՁɺධՁଛӹͷऔಘ item_name = ['อ༗ޱɹɹ |', 'औಘ୯Ձɹɹ |', 'ج४Ձֹɹɹ |',
'ධՁଛӹɹɹ |'] # ࢿ৴ୗ໊ͷऔಘ investmentname = [] investments = soup.find_all('td', class_='mbody', colspan="3") for investment in investments: investmentname.append(investment.a.text) inves_num = 0 row_num = 0 various_values = soup.find_all('tr', bgcolor='#eaf4e8', align="right") for various_value in various_values: # ࢿ৴ୗ໊ͷදࣔ print(investmentname[inves_num]) # อ༗ޱͷදࣔ print(item_name[row_num], various_value.td.text) for other in various_value.td.find_next_siblings("td"): row_num = row_num + 1 # อ༗ޱҎ֎ͷදࣔ print(item_name[row_num], other.text) row_num = 0 inves_num = inves_num + 1 except NoSuchElementException as e: print("seleniumͷૢ࡞தʹΤϥʔ͕ൃੜ͠·ͨ͠ɻ") traceback.print_exc() finally: # શͯͷΫοΩʔΛআ driver.delete_all_cookies() driver.quit() if __name__ == "__main__": main() मਖ਼Օॴ
݁
݁ w εΫϨΠϐϯάඞͣޭ͢ΔͱݶΒͳ͍ w ྫ֎ॲཧ USZFYDFQUpOBMMZ ͕ඞཁ w ͍ͭͰਖ਼ৗऴྃͤ͞Δॲཧ͕ඞཁ