查找最接近HTML的字符串的电子邮件

时间:2015-02-21 16:23:10

标签: python html regex

我正在寻找一种方法来查找最接近的电子邮件地址(因此,页面文本中的电子邮件或mailto标记中的地址)到HTML文档中的某个字符串。到最近,我的意思是密钥字符串和电子邮件之间的字数最少。

到目前为止我的代码:

import urllib,re

key_str = "King of the World"

htmlFile = urllib.urlopen("http://www.somewebsite.com")
html = htmlFile.read()

best_match = find_closest(key_str, html)

find_closest()是我不确定如何创建的功能。理想情况下,它应该返回与html文本中第一次出现key_str的单词距离最接近的电子邮件。

我考虑使用正则表达式来做这件事,但我的正则表达式技能并不能完成这项任务。另外,我的代码库的其余部分是在python中,所以python解决方案对我来说是理想的,但是如果有人有更好的建议,我会完全开放。

编辑:

为了澄清我正在寻找的内容,我目前的用例是尝试在目录页面上查找某个人的电子邮件。所以,例如,假设我想找到该人的电子邮件" email_two"在网站目录中。该目录的示例HTML可能如下所示:

...
<h2>Site Operator</h2>
<p>(555) 555-5555</p>
<h2>Email One</h2>
<p><a href="mailto:dont_email_me.admissions@test.edu">dont_email_me.admissions@test.edu</a><br>(555) 555-5555<br> 1000 Address St.<br>Ice Pole</p>
<h2>Email Two</h2>
<p><a href="mailto:email_me@test.edu">email_me@test.edu</a><br>(555) 555-5555<br>Example Place<br>Example, EX</p>
<h2>Website Feedback</h2>
...

如果我跑find_closest('Email Two', html),我希望收到email_me@test.edu。

我不希望能够制作一个一直都是正确的系统,但是将最接近的电子邮件发送到我正在寻找的字符串似乎是最准确的方法。

1 个答案:

答案 0 :(得分:1)

我永远不会用正则表达式解析HTML,因为它不是常规的。我会使用像BeautifulSouplxml这样的XML / HTML解析器。这是一个使用我个人最喜欢的BeautifulSoup

的例子
import urllib
from bs4 import BeautifulSoup


def find_closest(keystr, email):
    wordList = keystr.split(' ')
    for word in wordList:
        if word.lower() in email:
            return True
    return False


key_str = "A Snowman is fun to build"

response = urllib.urlopen("http://www.builtbysnowman.com/")
htmlText = response.read()

bs = BeautifulSoup(htmlText)

emailCount = 0
for t in bs.findAll('a'):
   if (('mailto:' or '@') in t['href']) and emailCount == 0:
        closeMatch = find_closest(key_str, t['href'])
        if closeMatch:
            emailCount = emailCount + 1
            print t['href']

修改

根据您的问题编辑,这是我更新的解决方案:

import urllib
from bs4 import BeautifulSoup


def find_closest(keystr, htmlText):

    bs = BeautifulSoup(htmlText)

    emailCount = 0
    for t in bs.findAll('h2'):
        if ((t.text == keystr) and (emailCount == 0)):
            a = t.findNext('p').findNext('a')
            return a['href']


#key_str = "Email Two"

#Connect to url:

#response = urllib.urlopen("http://www.builtbysnowman.com/")
#htmlText = response.read()

htmlText = '''
<html>
    <title>
    Directory Page
    </title>
    <body>
        <h2>Site Operator</h2>
        <p>(555) 555-5555</p>
        <h2>Email One</h2>
        <p><a href="mailto:dont_email_me.admissions@test.edu">dont_email_me.admissions@test.edu</a><br>(555) 555-5555<br> 1000 Address St.<br>Ice Pole</p>

        <h2>Email Two</h2>
        <p><a href="mailto:email_me@test.edu">email_me@test.edu</a><br>(555) 555-5555<br>Example Place<br>Example, EX</p>
        <h2>Website Feedback</h2>

        <h2>Email Three</h2>
        <p><a href="mailto:email_me_sometimes@test.edu">email_me@test.edu</a><br>(555) 555-5555<br>Example Place<br>Example, EX</p>
        <h2>Website Moderator</h2>


    </body>
</html>
'''

print find_closest('Email Two', htmlText)
print find_closest('Email One', htmlText)
print find_closest('Email Three', htmlText)