python3.6のCGIでGoogleニュースのヘッドラインを抽出するスクレイピング例


python3.6によるスクレイピング例

Googleニュースのヘッドラインのタイトルを抽出する。

beautifulsoupは使わず、正規表現でゴリゴリする。


-------
#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests
import re
import sys
import cgi
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

my_title = 'what\'s hot'

def print_head(title):
    print('Content-type: text/html')
    print()
    print('<html><head>')
    print('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">')
    print('<title>'+title+'</title>')
    print('</head>')

def print_tail():
    print('</html>')

print_head(my_title)

print('<body>')

res = requests.get('https://news.google.com/?hl=ja&gl=JP&ceid=JP%3Aja')
res.raise_for_status()

print("From Google News JP status:" + str(res.status_code) + "<br><br>")

tmp = re.sub('\n','',res.text)
tmp = re.sub('<a href','\n<a href',tmp)
tmp = re.sub('\/a>','/a>\n',tmp)
tmplist = tmp.split('\n')

result = [ s for s in tmplist if re.match('<a href=\"\.\/articles.*?\/a>', s) or ( 'すべての記事' in s ) ]

for i in result:
    if 'すべての記事' in i:
        break
    s = re.sub(r'<a href.*?<span >(.*?)<\/span><\/a>',r'\1',i)
    print(s+"<br>")

print('</body>')

print_tail()

------

苦労したところ

import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

これを入れないと、抽出した結果をprintするときに下記のようなエラーになる。

UnicodeEncodeError: 'ascii' codec can't encode characters in position 343-391: ordinal not in range(128)

googleニュースのヘッドラインだけを抽出したかったのだが、
htmlソースを見ても特定の条件がみつからない。

'<a href=\"\.\/articles.*?\/a>',

で抽出すれば見出しのリンクが取れるのだが、ヘッドライン以外もすべて取れてしまう。

「すべての記事」というキーワードが区切りになっているので、
それが出てくるまで、という条件で抽出した。


これができればどこからでもなんでも取れる....








python3のhello world cgi

最小限のpython cgi

-----
#!/usr/bin/python
# -*- coding: utf-8 -*-

my_title = 'greeting from python'
my_message = 'hello, world'

###

def print_head(title):
    print('Content-type: text/html')
    print()
    print('<html><head>')
    print('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">')
    print('<title>'+title+'</title>')
    print('</head>')

def print_body(message):
    print('<body>')
    print(message)
    print('</body>')

def print_tail():
    print('</html>')

###

print_head(my_title)

print_body(my_message)

print_tail()
-----


3系はprintにカッコをつけてつかう

インデントでスペースとタブを混在させてはいけない


python3をデフォルトにする@centos6

google analyticsがあるcgiがinternal server errorになっていることを報告してきて、
それを直している過程で、

perl (LWP)でやっているスクレイピングが時代遅れ

pythonの2系は時代遅れ

ということを悟ったので、3系(3.6)をインストールした。

2.6とか2.7とかいろいろ入ってしまったのできれいにしようと思ったのだが、
うまく消えないのでとりあえず3系がデフォルトで動くようにした。



# pip install beautifulsoup4
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Requirement already satisfied: beautifulsoup4 in /usr/local/lib/python2.7/site-packages (4.7.1)
Requirement already satisfied: soupsieve>=1.2 in /usr/local/lib/python2.7/site-packages (from beautifulsoup4) (1.8)
Requirement already satisfied: backports.functools-lru-cache; python_version < "3" in /usr/local/lib/python2.7/site-packages (from soupsieve>=1.2->beautifulsoup4) (1.5)
#


beautifulsoupなる便利なものがあると聞いて使ってみようとしたら「まだ2.7なんか使ってんのかよ」と怒られた。


# python --version
Python 2.6.6


python

とだけやると、2.6.6が起動する。

python3.6

とやれば3系が動くようにはなっているが、面倒。

# python3.6
Python 3.6.7 (default, Dec  5 2018, 15:02:16)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

"python" はシンボリックリンクなので、
それを3系にリンクしなおせばよい。

# which python
/usr/bin/python

# which python3.6
/usr/bin/python3.6


pipも

# which pip3.6
/usr/bin/pip3.6

# which pip
/usr/local/bin/pip



# ln -s /usr/bin/python3.6 /usr/bin/python
ln: creating symbolic link `/usr/bin/python': ファイルが存在します

現在のリンクを消してから3系へのリンクを作る。

# unlink /usr/bin/python

# ln -s /usr/bin/python3.6 /usr/bin/python



# ln -s /usr/bin/pip3.6 /usr/local/bin/pip
ln: creating symbolic link `/usr/local/bin/pip': ファイルが存在します

pipも同様

# unlink /usr/local/bin/pip
# ln -s /usr/bin/pip3.6 /usr/local/bin/pip




# python
Python 3.6.7 (default, Dec  5 2018, 15:02:16)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>


# pip -V
pip 9.0.1 from /usr/lib/python3.6/site-packages (python 3.6)


pythonもpipも3系になった。