2016-12-23 5 views
2

私はBeautifulSoupライブラリで遊んでいます。私はウェブサイトからメールを解析しようとしていましたが、予期せぬ結果を得ました。これは私のコードです:BeautifulSoupと正規表現で解析すると予期しない結果が発生する

from urllib.request import urlopen 
from urllib.error import HTTPError 
from urllib.error import URLError 

from bs4 import BeautifulSoup 
import re 
from urllib.parse import quote 

startUrl = "http://getrocketbook.com/pages/returns" 
try: 
    html = urlopen(quote((startUrl).encode('utf8'), ':/?%#_')) 
    bsObj = BeautifulSoup(html, "html.parser") 
    alls = bsObj.body.findAll(text=re.compile('[A-Za-z0-9\._+-][email protected][A-Za-z0-9\.-]+')) 
    for al in alls: 
     print(al) 
except HTTPError: 
    pass 
except URLError: 
    pass 

は、私はちょうど電子メールを解析することが期待が、私は実際に解析され、これは代わりに宣告:私が間違っていることができるもの

If you’ve done all of this and you still have not received your refund yet, please contact us at [email protected] 

任意のアイデアを?

答えて

4

これは、findAll()が別の単語ではなく、実際の要素またはテキストノードを検索するためです。あなたがする必要がどのような

結果に同じコンパイル済みの正規表現を適用することです。また

pattern = re.compile('[A-Za-z0-9\._+-][email protected][A-Za-z0-9\.-]+') 
alls = bsObj.body.find_all(text=pattern) 
for al in alls: 
    print(pattern.search(al).group(0)) 

、単一の電子メールがあなたの代わりにfind()メソッドを使用することができるかどうか、そこにあるからです。

関連する問題