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を確認しましたが、何らかの理由でコード内で動作しません。
はい、ありがたくしています。 – pratibha