2017-03-06 20 views
0

Webページから電子メールを抽出するための基本的なスクリプトを書きました。BeautifulSoup電子メールの抽出が機能しない

from bs4 import BeautifulSoup 
import requests, re 

def get_email(url): 
    response = requests.get(url, headers={ 
     'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36', 
     'Upgrade-Insecure-Requests': '1', 'x-runtime': '148ms'}, allow_redirects=True).content 

    soup = BeautifulSoup(response, "html.parser") 

    email = soup(text=re.compile(r'^[a-zA-Z]+[\w\-.][email protected][\w-]+\.[\w.-]+[a-zA-Z]')) # this is working with 

    print ("email ",email) 


get_email('http://www.aberdeenweddingshop.co.uk/contact-us') 
get_email('http://www.foodforthoughtdeli.co.uk/contact.htm') 

OUTPUT: 
email [email protected] 
email [] <------------------------#should give [email protected] 

最初のURLでは正しい結果が得られますが、2番目のURLでは何も取得されません。私はその理由を知らない。私は正規表現も変更しようとしました。正規表現hereを確認しましたが、何らかの理由でコード内で動作しません。

答えて

1

最初のケースでは、電子メールは単一スパンのテキストです。あなたの2番目のケースでは、電子メールはp要素にあり、電子メールよりも多くのテキストを持っています。

あなたの正規表現は、文字列の先頭から検索されているため、指定されたコンテキストでは有効ではない文字のため、2番目に一致しません。

という文字列を見つけてから抽出する必要があります。 例:

from bs4 import BeautifulSoup 
import requests, re 

def get_email(url): 
    response = requests.get(url, headers={ 
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36', 
    'Upgrade-Insecure-Requests': '1', 'x-runtime': '148ms'}, allow_redirects=True).content 

    soup = BeautifulSoup(response, "html.parser") 

    email = soup(text=re.compile(r'[A-Za-z0-9\.\+_-][email protected][A-Za-z0-9\._-]+\.[a-zA-Z]*')) 

    _emailtokens = str(email).replace("\\t", "").replace("\\n", "").split(' ') 

    if len(_emailtokens): 
     print([match.group(0) for token in _emailtokens for match in [re.search(r"([a-zA-Z0-9_.+-][email protected][a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)", str(token.strip()))] if match]) 


get_email('http://www.aberdeenweddingshop.co.uk/contact-us') 
get_email('http://www.foodforthoughtdeli.co.uk/contact.htm') 

出力:

[ '[email protected]']

[ '[email protected]']

+0

はい、ありがたくしています。 – pratibha

1

2番目のURLと一致しない場合は、キャレット(^)が正規表現を最初に必要とするためです。キャレットを省略した場合、以下が得られる:

>>> soup(text=re.compile(r'[a-zA-Z]+[\w\-.][email protected][\w-]+\.[\w.-]+[a-zA-Z]')) 
['E-mail: \n\t\t\t\t\t\t\t\t\t\t\t\t\[email protected]\n\t\t\t\t\t\t\t\t\t\t\t\t\t'] 

我々は応じて文字列にマッチする正規表現を使っているので、私たちは本当に美しいスープの良い部品を使用していないと、それは省略することができます完全:

def get_email(url): 
    response = requests.get(url, headers={ 
     'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36', 
     'Upgrade-Insecure-Requests': '1', 'x-runtime': '148ms'}, allow_redirects=True).content 
    response = requests.get(url, headers = headers, allow_redirects=True).text 
    email_address = re.search(r'[a-zA-Z]+[\w\-.][email protected][\w-]+\.[\w.-]+[a-zA-Z]', response).group() 
    print(email_address) 

注:私は、文字列表現で動作するようにレスポンスオブジェクトのtext属性を使用 - というよりもcontent属性を使用して戻されたバイトストリーム。

関連する問題