###前提・実現したいこと
現在、chrome拡張機能を作っています。ユーザーが見たページのURLをサーバー側に送って、そのURL先のページの本文を抽出しそれをmecabで形態素分析したいです。
###発生している問題・エラーメッセージ
webから本文を抽出することと、自分で用意した例文を形態素分析し名詞をmysqlに送ることには成功しました。しかし、抽出した本文を形態素分析してmysqlに送ることがありません。
また、printすると以下のようなエラーが出ていました。
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbb in position 2: invalid start byte
そこで、スクレイピングしたtextの型を調べてみましたが、str型で特に問題になるとは思えません。(形態素分析できた例文もstr型です。)
###該当のソースコード
pythonをcgiとして実行しました。
python
1#!/usr/local/bin/python3.6 2 3print("Content-Type: text/html; charset=UTF-8") 4print() 5 6import cgi 7import cgitb; cgitb.enable() 8import json 9#import bs4 10#import urllib.request, urllib.parse 11from readability.readability import Document 12import urllib 13import html2text 14import MeCab 15import mysql.connector 16import re 17 18form = cgi.FieldStorage() 19for key in form.keys(): 20 url = form.getvalue(key) 21 html = urllib.request.urlopen(url) 22 s = html.read() 23 article = Document(s).summary() 24 text = html2text.html2text(article) 25 text = text.strip("") 26 text = text.replace("http","") 27 text = text.replace("/","") 28 text = text.replace("[","") 29 text = text.replace("]","") 30 text = text.replace("%","") 31 text = text.replace(" ","") 32 text = text.replace("{","") 33 text = text.replace("}","") 34 text = text.replace("%","") 35 text = text.replace(":","") 36 text = text.replace("#","") 37 text = text.replace("*","") 38 text = text.replace("\n","") 39 text = text.replace("|","") 40 text = text.replace("-","") 41 text = text.replace(".","") 42 text = text.replace("?","") 43 text = text.replace("(","") 44 text = text.replace(")","") 45 text = text.replace("<","") 46 text = text.replace(">","") 47 text = text.replace("¥¥","") 48 text = text.replace("¥n","") 49 text = text.replace("1","") 50 text = text.replace("2","") 51 text = text.replace("3","") 52 text = text.replace("4","") 53 text = text.replace("5","") 54 text = text.replace("6","") 55 text = text.replace("7","") 56 text = text.replace("8","") 57 text = text.replace("9","") 58 text = text.replace("0","") 59 text = text.replace("@","") 60 text = text.replace("\◆","") 61 text = text.replace("◆","") 62 63 tagger = MeCab.Tagger('-d ./mecab-ipadic-neologd') 64 tagger.parse('') 65 input = text 66 result = tagger.parseToNode(input) 67 node = tagger.parseToNode(input) 68 target_parts_of_speech = ('名詞', ) 69 words = [] 70 print(node.feature) 71 while node: 72 if node.feature.split(',')[0] in target_parts_of_speech: 73 print(type(node.surface)) 74 words.append(node.surface) 75 node = node.next 76 word = ','.join(words) 77 config = { 78 'user': 'root', 79 'password': 'root', 80 'unix_socket': '/Applications/MAMP/tmp/mysql/mysql.sock', 81 'database': 'hoge', 82 'raise_on_warnings': True, 83 } 84 link = mysql.connector.connect(**config) 85 cursor = link.cursor() 86 87 cursor.execute('''insert into horizon (url,text) values (%s,%s)''', [url,word]) 88 link.commit() 89 90 cursor.execute("select * from horizon;") 91 for row in cursor.fetchall(): 92 print(row[0],row[1],row[2]) 93 94 cursor.close() 95 link.close()
###試したこと
スクレイピングしてきた情報には無駄な記号や空白がたくさんあり、text = ""に入れても文字列として認識されませんでした。そのため、replaceで文章をシンプルにするようにしてみたものの、まだmecabは反応してくれません。
###補足情報(言語/FW/ツール等のバージョンなど)
開発環境は以下の通りです。
- macOS Sierra
- python3
- MAMP バージョン4.2
回答5件
あなたの回答
tips
プレビュー