在带有bs4的python中使用regex从脚本中刮取电子邮件地址

2024-09-30 12:25:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图从一个网站上抓取电子邮件地址,在这个网站上,电子邮件嵌套在一个脚本中,而一个简单的“find/findAll+.text”并不能奏效。你知道吗

源html:

<script>EMLink('com','aol','mikemhnam','<div class="emailgraphic"><img style="position: relative; top: 3px;" src="https://www.naylornetwork.com/EMailProtector/text-gif.aspx?sx=com&nx=mikemhnam&dx=aol&size=9&color=034af3&underline=yes" border=0></div>','pcoc.officialbuyersguide.net Inquiry','onClick=\'$.get("TrackLinkClick", { LinkType: "Email", LinkValue: "mikemhnam@aol.com", MDSID: "CPC-1210", AdListingID: "" });\'')</script> <br/>

我目前的方法是尝试“findAll+”regex表达式,如下所示:

for email in soup.findAll(class_='ListingPageNameAddress NONE'):
    print(email.findAll("([\w\._]+\@([\w_]+\\.)+[a-zA-Z]+)"))

但在jupyter中,这只是返回一个[] :/

正则表达式有问题吗?或者用一种更简单的方法来梳理邮件?你知道吗


Tags: 方法textdiv脚本com网站电子邮件email
2条回答

似乎您没有使用正确的findall方法。您需要import re,然后使用^{}方法,而不是findAll()方法(注意字母“A”的大小写差异)。函数的接口是:

re.findall(pattern, string, flags=0)

有关详细信息,请参见re文档中的this section查找所有副词。你知道吗

尽管regex可能会随着时间的推移而变得更加健壮,但根据我的经验,脚本标签的这些部分保持相当稳定,所以考虑使用split的计划B

html ='''

<script>EMLink('com','aol','mikemhnam','<div class="emailgraphic"><img style="position: relative; top: 3px;" src="https://www.naylornetwork.com/EMailProtector/text-gif.aspx?sx=com&nx=mikemhnam&dx=aol&size=9&color=034af3&underline=yes" border=0></div>','pcoc.officialbuyersguide.net Inquiry','onClick=\'$.get("TrackLinkClick", { LinkType: "Email", LinkValue: "mikemhnam@aol.com", MDSID: "CPC-1210", AdListingID: "" });\'')</script>
<br/>

'''

print(html.split('LinkValue: "')[1].split('"')[0])

相关问题 更多 >

    热门问题