phemail py no attribute encode

2024-09-30 02:33:57 发布

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

root@bt:~# ./phemail.py -g0@*******.com
Gathering emails from domain: ******.com
Traceback (most recent call last):
  File "./phemail.py", line 206, in <module>
  gatherEmails(domain[0],domain[1],p)
  File "./phemail.py", line 51, in gatherEmails
  namesurname = re.sub(' -.*','',a.text.encode('utf8'))
AttributeError: 'NoneType' object has no attribute 'encode'

为什么a.text是非类型类型?在


Tags: textinpycom类型domainlineroot
3条回答

a.text没有值(None
初始化a变量的行可能有问题。在

顺便说一句,我不会建议你把事情当作根来做。在

作为解释,这个脚本所做的是使用谷歌搜索LinkedIn的索引页面,特别是搜索出现用户名的页面(与公司简介、工作、讨论等相反)。由于目标公司名称和可能是该公司的标准电子邮件格式是已知的(并在脚本的参数中指定),搜索似乎会搜索所有提到该公司的LI配置文件页面结果,提取名称,并从名称中生成电子邮件地址。它不是刮取电子邮件地址,甚至不是域名,而是刮取名字。在

这实际上表明了对LI如何使公共配置文件对搜索引擎可见(或者对很多垃圾结果的容忍度)缺乏理解,因为你的结果将充满“目录”页面,而不是配置文件。在

但除了这个策略性错误,你还错用了这个脚本——谷歌不支持每字符通配符——通配符主要表示一个或多个单词可能位于其他单词之间(或之后/之前——但它在两个单词之间效果最好)。不过,通配符行为有点棘手,并不是所有情况都有完整的文档记录。因此,即使以后没有失败,你的输出将是出现在LinkedIn一个非常通用的“site:”搜索中的前100个名字(没有任何公司/域名信息)。不知道这对谁有用吗?在

至于为什么脚本在该行上失败,您将迭代美丽之旅芬德尔调用搜索结果项的a标记。在本例中,a.text的值和类型为“None”,这会导致错误,因为None没有encode()方法。beauthoulsoup有很多很好的捷径,但是要追踪错误,它们可能会令人困惑。findAll的结果是一组标记,而标记的默认行为类似于findAll,所以我认为a.text就像在该交互循环的单个标记上调用findAll('text')。我不能确定为什么这不起作用-我在这台机器上没有beauthulsoup-但是你应该可以玩一下这个,看看哪里出了问题。在

相关部分:

user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
headers={'User-Agent':user_agent,}
p = 10

def gatherEmails(l,domain,p):
    print "Gathering emails from domain: "+domain
    emails = []
    for i in range(0,p):
        url = "http://www.google.co.uk/search?hl=en&safe=off&q=site:linkedin.com/pub+"+re.sub('\..*','',domain)+"&start="+str(i)+"0"
        request=urllib2.Request(url,None,headers)
        response = urllib2.urlopen(request)
        data = response.read()
        html = BeautifulSoup(data)
        for a in html.findAll('a',attrs={'class':'l'}):
            namesurname = re.sub(' -.*','',a.text.encode('utf8'))
            firstname = re.sub(' ([a-zA-Z])+','',namesurname).lower()
            surname = re.sub('([a-zA-Z])+ ','',namesurname).lower()
            sys.stdout.write("\r%d%%" %((100*(i+1))/p))
            sys.stdout.flush()
            if firstname != surname and not re.search('\W',firstname) and not re.search('\W',surname):                
                if l == '0' : # 1- firstname.surname@example.com
                    emails.append(firstname+" "+surname)

您使用的是Beautiful Soup 3.0.8之前的版本。升级至get.text、.getText(分隔符)和(在Beautiful Soup 4中)。get_text(分隔符)。在

相关问题 更多 >

    热门问题