2016-08-07 9 views
2

Beautiful Soup in Pythonを使用して文字列を含むすべての要素を検索します。Beautiful Soup findペルシャ語の文字列を使用してください。

ペルシア語以外の文字を使用すると動作しますが、ペルシア語の文字は使用しません。上記のコードの場合

from bs4 import BeautifulSoup 
QUERY = 'رشته فارسی' 
URL = 'http://www.example.com' 
headers = { 
    'User-Agent': "Mozilla/5.0 . . . " 
} 
request = urllib2.Request(URL, headers=headers) 
response = urllib2.urlopen(request) 
response_content = response.read().decode('utf8') 
soup = BeautifulSoup(response_content, 'html.parser') 
fetched = soup.find_all(text=QUERY) 
print(fetched) 

、出力は[]ですが、私は、クエリでASCIIを使用する場合それが動作します。

UTF-8変換などがありますか?:)

+0

ページのエンコーディングと一致する必要があります –

+0

@PadraicCunninghamどのようにすればいいですか? – masoud

+0

utf8の代わりにutf-8またはUTF-8を使用しないでください。 –

答えて

1
#-*- coding: utf-8 -*- 
    import urllib2 
    from bs4 import BeautifulSoup 
    QUERY = 'خدمات' 
    URL = 'https://bayan.ir/service/bayan/' 
    headers = { 
      'User-Agent': "Mozilla/5.0 . . . " 
    } 
    request = urllib2.Request(URL, headers=headers) 
    response = urllib2.urlopen(request) 
    response_content = response.read() 
    soup = BeautifulSoup(response_content, 'html.parser') 
    fetched = soup.find_all(string=QUERY) 
    print(fetched) 

+0

**正確な文字列を送信する必要があります** [BeautifulSoup](https://www.crummy.com/software/BeautifulSoup/bs4/doc/#the-string-argument)を確認してください。 – RaminNietzsche

関連する問題