0

我正在尝试将 for 循环的输出分配给变量或字典,但到目前为止,我只能让它打印循环的第一次迭代,而且它的格式甚至都不正确。

这是我的代码:

result, data = mail.uid('search', None, "(FROM 'tiffany@e.tiffany.com')") # search and return uids instead
latest_email_uid = data[0].split()[-1]
result, data = mail.uid('fetch', latest_email_uid, '(RFC822)')
raw_email = data[0][1]

html = raw_email
soup = BS(html)
pretty_email = soup.prettify('utf-8')

urls={}
for x in soup.find_all('a', href=True):
    urls['href'] = x

print urls

我希望这个输出的格式是这个代码的执行方式,但是所有这些 odes 都是正确打印出提取的链接:

result, data = mail.uid('search', None, "(FROM 'tiffany@e.tiffany.com')") # search and return uids instead
latest_email_uid = data[0].split()[-1]
result, data = mail.uid('fetch', latest_email_uid, '(RFC822)')
raw_email = data[0][1]

html = raw_email
soup = BS(html)
pretty_email = soup.prettify('utf-8')

for urls in soup.find_all('a', href=True):
    print urls['href']

print urls

谢谢!

编辑:

我希望它打印的方式是这样的:

3D"http://elink.tiffany.com/r/YB7DL5S/32FU1/5A6EIF/QFMQOO/9SUZ8/52/h"=
3D"http://elink.tiffany.com/r/YB7DL5S/32FU1/5A6EIF/QFMQOO/N8ASK/52/h"=
3D"http://elink.tiffany.com/r/YB7DL5S/32FU1/5A6EIF/QFMQOO/DNH42/52/h"=
3D"http://elink.tiffany.com/r/YB7DL5S/32FU1/5A6EIF/QFMQOO/T2WPJ/52/h"=
3D"http://elink.tiffany.com/r/YB7DL5S/32FU1/5A6EIF/QFMQOO/PO7RQ/52/h"=
3D"http://elink.tiffany.com/r/YB7DL5S/32FU1/5A6EIF/QFMQOO/BRLMA/52/h"=
3D"http://elink.tiffany.com/r/YB7DL5S/32FU1/5A6EIF/QFMQOO/N8ASQ/52/h"=
3D"http://elink.tiffany.com/r/YB7DL5S/32FU1/5A6EIF/QFMQOO/SV4PN/52/h"=
3D"http://elink.tiffany.com/r/YB7DL5S/32FU1/5A6EIF/QFMQOO/RC53N/52/h"=
3D"http://elink.tiffany.com/r/YB7DL5S/32FU1/5A6EIF/QFMQOO/7Q3AA/52/h"=

通过下面的解决方案,我得到了这个:

http://elink.tiffany.com/r/YB7DL5S/32FU1/5A6EIF/QFMQOO/1XF33/52/h?='n:underline="" style='3D"text-decoratio='>点击这里,http: //elink.tiffany.com/r/YB7DL5S/32FU1/5A6EIF/QFMQOO/6EN2U/52/h"='target='3D"_blank"'>http://eimg.tiffany.com/mbs_tiffanyc/Standard/Logoblue .gif"' title='3D"Tiffany' wi="dth=3D147"/>, http://elink.tiffany.com/r/YB7DL5S/32FU1/5A6EIF/QFMQOO/T2WUY/52/h"=' lucida ="" sans="" style='3D"文本装饰:无;' unicode="">订婚,

4

2 回答 2

1

您正在创建一个只有一个键的字典,您为循环的每次迭代覆盖该键。也许列表更有意义?

urls = []
for x in soup.find_all('a', href=True):
    urls.append(x)
于 2013-10-11T02:25:39.090 回答
1

您一直在字典中分配相同的键,这是您唯一的错误:

收集并放入列表:

urls=[]
for x in soup.find_all('a', href=True):
    urls.append(x['href'])
print urls

或者进入字典:

urls={}
search = soup.find_all('a', href=True)
for x in range(len(search)):
    urls[x] = search[x]['href'] ##Result is: {0:firsturl, 1:secondurl, 2:thirdurl}
    ##and so on

这应该可以按您的意愿工作,希望对您有所帮助!

于 2013-10-11T02:27:41.773 回答