LoginSignup
0
3

More than 3 years have passed since last update.

Python3 Webスクレイピング 実践入門

Posted at

https://qiita.com/Azunyan1111/items/9b3d16428d2bcc7c9406
この記事を参考にしたところ、python2ではめっちゃ動いて助かったけど、python3では微妙な違いで動かなかったので。
微妙な違い:print文とurllib2のところ。

python3のpipでbeautifulsoupをインストール

pip3 install beautifulsoup4

日経新聞のタイトルを取ってる来るコードの件

getNikkeiWebPageTitle.py
#!/usr/bin/env python3.6.9
# -*- coding: utf-8 -*-
# ref 1. https://qiita.com/Azunyan1111/items/9b3d16428d2bcc7c9406
# ref 2. https://qiita.com/rmanzoku/items/3bf4eb2b8b499ecb3dcf
import urllib.request
from bs4 import BeautifulSoup

# アクセスするURL
url = "http://www.nikkei.com/"

# URLにアクセスする htmlが帰ってくる → <html><head><title>経済、株価、ビジネス、政治のニュース:日経電子版</title></head><body....
html = urllib.request.urlopen(url)

# htmlをBeautifulSoupで扱う
soup = BeautifulSoup(html, "html.parser")

# タイトル要素を取得する → <title>経済、株価、ビジネス、政治のニュース:日経電子版</title>
title_tag = soup.title

# 要素の文字列を取得する → 経済、株価、ビジネス、政治のニュース:日経電子版
title = title_tag.string

# タイトル要素を出力
print(title_tag)

# タイトルを文字列を出力
print(title)

result

sharo@kirima:~$ python3 getNikkeiWebPageTitle.py 
<title>日本経済新聞</title>
日本経済新聞

おけまる水産。

reference

0
3
0

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
0
3