2016-12-12 6 views
0

私は私のウェブサイトからページのdiscriptionを読みたいです。ここでは、コードですPythonどのようにウェブからポルトガル語の文字を取得する

import urllib.request 
import re 
req = urllib.request.Request("http://sorelogios.pt", headers={'User-Agent': 'Mozilla/5.0'})   
htmltext = urllib.request.urlopen(req).read() 
if htmltext is None: 
    print("nada") 
else: 
    regex='<title>(.+?)</title>' 
    pattern=re.compile(regex) 
    price=pattern.findall(str(htmltext))  
    print(price[0]) 
    regex='<meta name="description" content=(.+?)/>' 
    pattern=re.compile(regex) 
    prices=pattern.findall(str(htmltext)) 
    print(prices[0]) 

私の問題は私のWebページにはポルトガルの文字を持っており、私はこの問題を解決し、元の文字を取得することができますどのように私は REL \ XC3 \ xb3gios homemロハオンライン のような何かを得るのですか? ありがとう

+0

'str()'、* decode *は使わないでください。正しい正規表現ではなく、[BeautifulSoup](https://www.crummy.com/software/BeautifulSoup/bs4/doc/)のような適切なHTMLパーサーを使用すると、Unicodeの結果が無料で返されます。 –

答えて

0

レスポンスをデコードする必要があります。

htmltext = urllib.request.urlopen(req).read().decode('utf-8') 
+0

正しい方向に私を指摘していただきありがとうございますが、私は落書きの問題があります:char \\ r \\ n \\ tのためにそれを実行すると空の結果が得られます –

+0

Martijn Pietersのコメントを見てください。もっと簡単に... –

関連する問題