LoginSignup
2
1

More than 3 years have passed since last update.

Python+Anaconda環境でスクレイピングのテスト

Posted at

Python+Anaconda環境でWebスクレイピングのテストコードを走らせるまでのメモ

Beautifulsoupのインストール

conda install beautifulsoup4
※pipインストールと一緒

サンプルコード

scraping.py
import requests
from bs4 import BeautifulSoup
import re

target_url = 'https://www.yahoo.co.jp/'

result = requests.get(target_url)
data_soup = BeautifulSoup(result.text, 'lxml')
elements = data_soup.find_all(href=re.compile("news.yahoo.co.jp/pickup"))
for e in elements:
    print(e.getText() + '\n' )

結果のサンプル

梅雨明け カギを握る台風5号
日韓関係悪化 歴史的転換点か
息子の叫び 体罰から救った親
高級車 大阪経由で北へ密輸か
ディーン&デルーカ 米で苦境
異常事態 高安休場で大関不在
こんまり 米エミー賞候補入り
訃報発表 流されたSMAP映像

参考

2
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1