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>',

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

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


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