【发布时间】:2013-04-09 15:28:30
【问题描述】:
我正在尝试查找给定页面上的所有电子邮件并使用正则表达式匹配它们。我正在使用 BeautifulSoup 来获取所有标签
email_re = re.compile('[A-Za-z0-9\.\+_-]+@[A-Za-z0-9\._-]+\.[a-zA-Z]*')
email = soup.findAll("a")
for j in email:
email = j.string
for match in email_re.findall(email):
outfile.write(match + "\n")
print match
但是,当我运行我的脚本时,它的这一部分会得到一个 TypeError: expected string or buffer。我认为这是因为 email 是 BeautifulSoup 对象,而不是 python 字符串。我尝试使用 str() 或 str() 将其转换为字符串,并且都返回另一个错误:UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 9 :序数不在范围内(128)。我能做些什么来解决这些错误,并实际运行我的脚本。我没主意了。请帮忙!
【问题讨论】:
-
您使用哪个 Python 版本? 2.* 还是 3.*?
-
我使用的是 python 2.7
-
哪一行触发了错误?
outfile.write(match + "\n")?
标签: python regex beautifulsoup