我正在尝试使用这个python代码工作,它将从imap.laposte.net
(来自法国,我来自我居住的地方)检索我的电子邮件,然后按照邮件中给出的链接,然后发送返回到指定页面之一的电子邮件地址。
工作的第一部分已经没事了;我找到了几个关于在网上使用imaplib
,检索和选择我想要的内容的例子;我相信如何发送我将访问过的页面也很容易。
但问题出在那里,给定电子邮件中的链接,如何选择它,然后去访问给定的页面?
我在data[0].split()
中将该电子邮件检索为一种字符串,并使用Beautiful Soup处理该字符串,就好像它是àweb页面一样,以便从中提取电子邮件中包含的URL:
import imaplib, rfc822, sys
from bs4 import BeautifulSoup
server ='imap.laposte.net'
username='username'
password='VeryStrong'
M = imaplib.IMAP4(server)
M.login(username, password)
M.select()
typ, data = M.search(None, 'ALL')
for num in data[0].split():
typ, data = M.fetch(num, '(RFC822)')
pos1=data[0][1][0:1000].find('entre-infideles')
if pos1 != -1:
print '06ReadImap: Message %s' % (num)
pos2=data[0][1][pos1:].find('Subject')
pos3=data[0][1][pos1+pos2:].find('Subject: <PUB>')
pos4=data[0][1][pos1+pos2+pos3:].find('votre profil')
if pos4 != -1:
print '06ReadImap: Pos4(votre profil)=%i' % (pos2+pos3+pos4)
print data[0][1][pos1+pos2+pos3:pos1+pos2+pos3+pos4+12]
soup=BeautifulSoup(data[0][1])
for link in soup.find_all('a'):
print(link.get('href'))
sys.exit(0)
问题在于它给了我很多链接,当然,包含在电子邮件中的所有链接,但它们是不完整的,我不能用它们作为URL来“获取”任何HTLM内容的页面;它给出了:
$ python ./S.py
06ReadImap: Message 8
06ReadImap: Pos4(votre profil)=625
Subject: <PUB> salma311 a =?utf-8?Q?visit=C3=A9?= votre profil
3D"http://fr.supe=
3D"h=
3D"htt=
3D"http://fr.superboxy.me/tracking_unitary/2/111740993/=
3D"http://fr.super=
3D"http://fr.superboxy.me/tracking_unitary/2/111740993/=
...
..
.
如何从电子邮件中的链接中检索完整的网址? 非常感谢, 大卫
答案 0 :(得分:0)
您需要先撤消邮件的内容传输编码。这个似乎是在Quoted Printable编码中,这使您的HTML解析器感到困惑。
答案 1 :(得分:0)
# quoted_printable_decode python
result = quopri.decodestring(data[0][1])
#
soup=BeautifulSoup(result)
print "\n---------------- Extracting all the URLs found within page 1’s <a> tags :".encode('utf8')
i=0
for link in soup.find_all('a'):
i=i+1
print(link.get('href'))
在这里, d。