如何在站点上使用BeautifulSoup或Slimit从javascript变量输出电子邮件地址

时间:2014-12-29 02:36:42

标签: javascript python email parsing beautifulsoup

我有这个示例网站:http://www.example.com/whatever.asp?profile=1

对于每个个人资料编号,我在此Java脚本代码中有不同的电子邮件。

<script LANGUAGE="JavaScript">
function something()
{
var ptr;
ptr = "";
ptr += "<table><td class=france></td></table>";
ptr += "<table><td class=france><a href=mailto:exa";
ptr += "mple@email.com>email</a></td></table>";
document.all.something.innerHTML = ptr;
}
</script>

我想解析或正则表达电子邮件地址。电子邮件的位置取决于长度。但是使用这个python代码我只能解析mple@email.com而不是example@email.com

url=urllib.urlopen('http://www.example.com/whatever.asp?profile=1')
contents= url.read()   
soup = BeautifulSoup(contents)
js_content= soup.findAll("script")[0].text
reg = '(<)?(\w+@\w+(?:\.\w+)+)(?(1)>)'
match= re.search(reg,js_content)
print match.group()

有任何帮助吗?感谢。

2 个答案:

答案 0 :(得分:0)

#!/usr/bin/env python

from bs4 import BeautifulSoup
import re

soup = '''
<script LANGUAGE="JavaScript">
function something()
{
var ptr;
ptr = "";
ptr += "<table><td class=france></td></table>";
ptr += "<table><td class=france><a href=";
ptr += "mailto:example@knesset.com>email</a></td></table>";
document.all.something.innerHTML = ptr;
}
</script>
'''


soup = BeautifulSoup(soup)

for script in soup.find_all('script'):
    reg = '(<)?(\w+@\w+(?:\.\w+)+)(?(1)>)'
    reg2 = 'mailto:.*'
    secondHalf= re.search(reg, script.text)
    firstHalf= re.search(reg2, script.text)
    secondHalfEmail = secondHalf.group()
    firstHalfEmail = firstHalf.group()
    firstHalfEmail = firstHalfEmail.replace('mailto:', '')
    firstHalfEmail = firstHalfEmail.replace('";', '')
    if firstHalfEmail == secondHalfEmail:
        email = secondHalfEmail
    else:
        if ('>') not in firstHalfEmail:
            if ('>') not in secondHalfEmail:
                if firstHalfEmail != secondHalfEmail:
                    email = firstHalfEmail + secondHalfEmail
            else:
                email = firstHalfEmail
        else:
            email = secondHalfEmail

    print email

答案 1 :(得分:0)

我建议您使用re.findall代替re.search,因为搜索只返回第一个匹配。

url=urllib.urlopen('http://www.example.com/whatever.asp?profile=1')
contents= url.read()   
soup = BeautifulSoup(contents)
js_content= soup.findAll("script")[0].text
reg = r'<?(\w+@\w+(?:\.\w+)+)>?'
match= re.findall(reg,js_content)