pythonでWebスクレイピングを行おうとしているのですが、出力が文字化けします。
下記のコードが文字化けしている出力です。sucraping_naver.pyのファイルを実行しているのですが、上側に文字が化けて出現します。
ÉüèÜ·Bl/html/__init__.py", line 866ÄÑIo³êܵ½B¼¿Éæ3àtÌgt (py2) user@user-CF-SX1GE2DC:~/py2/garbage$ ml) TypeError: exïÅAæ97ãÌàtes-like object (py2) åbÍo@user-CF-SX1GE2DC:~/py2/garbage$ python sucraping_naver.py @À{
コードは下記です。
python
1import lxml.html 2import cssselect 3import requests 4 5target_url = 'http://news.tv-asahi.co.jp/news_politics/articles/000041338.html' 6target_html = requests.get(target_url).text 7root = lxml.html.fromstring(target_html) 8n = root.cssselect("#news_body > p")[0].text 9print(n) 10
参考にしたのは下記のサイトです。
http://qiita.com/beatinaniwa/items/72b777e23ef2390e13f8
文字化け対策のサイトもいろいろとググッてみたのですが、修正できませんでした。
http://ja.stackoverflow.com/questions/11182/python%E3%81%A7web%E3%82%B5%E3%82%A4%E3%83%88%E3%81%8B%E3%82%89%E3%81%AEhtml%E5%8F%96%E5%BE%97%E3%82%92%E3%81%97%E3%81%BE%E3%81%97%E3%81%9F%E3%81%8C%E6%96%87%E5%AD%97%E5%8C%96%E3%81%91%E3%81%97%E3%81%BE%E3%81%99
どうすれば文字化けを修正できるのでしょうか?
回答2件
あなたの回答
tips
プレビュー