• Skip to main content
  • Skip to primary sidebar

学習記録

スクレイピング

APIから情報取得 スクレイピング

2017年11月30日 by 河副 太智 Leave a Comment

GOOGLE APIから情報を取得し、JSONファイルに保存

 

google drive参照

 

Filed Under: スクレイピング

datetime.now() 時間を取得

2017年11月30日 by 河副 太智 Leave a Comment

import time
from datetime import datetime

 

print([datetime.now(),value])

Filed Under: スクレイピング

time.sleep()

2017年11月30日 by 河副 太智 Leave a Comment

import time

for i in range(14):
time.sleep(1)
print(“1秒経過”)

Filed Under: スクレイピング

xml rssをスクレイピング

2017年11月30日 by 河副 太智 Leave a Comment

import urllib.request
from bs4 import BeautifulSoup

url = "http://www.customslegaloffice.com/fta/feed/"
xml = urllib.request.urlopen(url)

soup = BeautifulSoup(xml, "lxml")
pubdates = soup.find_all("title")#titleのみを取り出す

for pubdate in pubdates:
print(pubdate.text)#.textは要素の内部のみを取得<title>などは取得しない

Filed Under: スクレイピング

HTMLを読み込む

2017年11月30日 by 河副 太智 Leave a Comment

import urllib.request

url = “http://www.customslegaloffice.com/fta”

 

# urlopenを使う
html = urllib.request.urlopen(url)
#readの引数に文字数を指定.decode(“utf=8#))
print(html.read(500).decode(“utf-8”))

 

 

例1:yahooニュースからスクレイピング

 

import urllib.request
from bs4 import BeautifulSoup

url = "https://news.yahoo.co.jp"
html = urllib.request.urlopen(url)

soup = BeautifulSoup(html, 'lxml')

topics = soup.find_all("p",class_="ttl")

for topic in topics:
print(topic.text)

 

例:2 ビットコイン価格の取得

import urllib.request
from bs4 import BeautifulSoup
import csv
import time
from datetime import datetime

url = "https://coinmarketcap.com/currencies/bitcoin-cash/"

 

f = open("bitcoin.csv", "w")
writer = csv.writer(f, lineterminator="\n")

html = urllib.request.urlopen(url)
soup = BeautifulSoup(html, 'lxml')

for i in range(15):

# リスト形式で格納し、csvに書き込む
price = soup.find_all("span", class_="text-large2", id="quote_price")
value = price[0].text
writer.writerow([datetime.now(),value])

# 240秒処理を休止します
time.sleep(2)

f.close()

 

 

 

 

 

 

 

例:1で取得したyahooのスクレイピングの対象タグ

 

<li>
<div>
<p class=”ttl“><a href=”https://news.yahoo.co.jp/pickup/6263011” onmousedown=”this.href=’https://rdsig.yahoo.co.jp/_ylt=A2Rifg0JaR9aimsAUHAEnf57/RV=2/RE=1512094345/RH=cmRzaWcueWFob28uY28uanA-/RB=tJQRd1AC72Lz8jGDGZOagZ_Lbl0-/RU=aHR0cHM6Ly9uZXdzLnlhaG9vLmNvLmpwL3BpY2t1cC82MjYzMDExAA–/RK=0/RS=8TztB1eAvSrIMPzmJpmHQep2GB8-‘“>海底に人 墜落空自ヘリ乗員か<span class=”icPhoto“>写真</span></a></p>
</div>
</li>
<li>
<div>
<p class=”ttl“><a href=”https://news.yahoo.co.jp/pickup/6263008” onmousedown=”this.href=’https://rdsig.yahoo.co.jp/_ylt=A2Rifg0JaR9aimsAUXAEnf57/RV=2/RE=1512094345/RH=cmRzaWcueWFob28uY28uanA-/RB=tJQRd1AC72Lz8jGDGZOagZ_Lbl0-/RU=aHR0cHM6Ly9uZXdzLnlhaG9vLmNvLmpwL3BpY2t1cC82MjYzMDA4AA–/RK=0/RS=9uov33ITmDdzaj9DqnJQ7RhyFBE-‘“>火星15 米攻撃能力なお疑問<span class=”icPhoto“>写真</span></a></p>
</div>
</li>
<li>
<div>
<p class=”ttl“><a href=”https://news.yahoo.co.jp/pickup/6263017” onmousedown=”this.href=’https://rdsig.yahoo.co.jp/_ylt=A2Rifg0JaR9aimsAUnAEnf57/RV=2/RE=1512094345/RH=cmRzaWcueWFob28uY28uanA-/RB=tJQRd1AC72Lz8jGDGZOagZ_Lbl0-/RU=aHR0cHM6Ly9uZXdzLnlhaG9vLmNvLmpwL3BpY2t1cC82MjYzMDE3AA–/RK=0/RS=hpkwmxO3YnpTdJcl8hgYbThc.vI-‘“>賃金より給食高く 障害者訴え<span class=”icPhoto“>写真</span><span class=”icNew“>new</span></a></p>
</div>
</li>

Filed Under: スクレイピング

  • « Go to Previous Page
  • Page 1
  • Page 2

Primary Sidebar

カテゴリー

  • AWS
  • Bootstrap
  • Dash
  • Django
  • flask
  • GIT(sourcetree)
  • Plotly/Dash
  • VPS
  • その他tool
  • ブログ
  • プログラミング
    • Bokeh
    • css
    • HoloViews
    • Jupyter
    • Numpy
    • Pandas
    • PosgreSQL
    • Python 基本
    • python3
      • webアプリ
    • python3解説
    • scikit-learn
    • scipy
    • vps
    • Wordpress
    • グラフ
    • コマンド
    • スクレイピング
    • チートシート
    • データクレンジング
    • ブロックチェーン
    • 作成実績
    • 時系列分析
    • 機械学習
      • 分析手法
      • 教師有り
    • 異常値検知
    • 自然言語処理
  • 一太郎
  • 数学
    • sympy
      • 対数関数(log)
      • 累乗根(n乗根)
    • 暗号学

Copyright © 2025 · Genesis Sample on Genesis Framework · WordPress · Log in