2013-10-11 89 views
0

我想將for循環的輸出分配給變量或字典,但現在我只能打印出循環的第一個迭代,它是甚至沒有正確的格式。將for循環的輸出分配給一個變量

這裏是我的代碼:

result, data = mail.uid('search', None, "(FROM '[email protected]')") # 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 

我想這個輸出是如何執行了這段代碼的格式,但所有這一切的頌歌是打印出來的提取鏈接正確:

result, data = mail.uid('search', None, "(FROM '[email protected]')") # 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.tiffa ny.com/r/YB7DL5S/32FU1/5A6EIF/QFMQOO/T2WUY/52/h"='lucida =「」sans =「」style ='3D「text-decoration:none;' unicode =「>>參與,

+0

'我想這是這個代碼執行的輸出格式'恐怕我真的不明白這行或你想要的格式是什麼。也許你可以展示你想要的實際輸出的例子嗎? –

+0

輸出大約40個用美麗的名字分析的網址,用行 – metersk

回答

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 

這應該按照您的需要工作,希望這有助於您!

+0

我喜歡列表比字典好得多,但它不能正確解析...請參閱我的編輯到我原來​​的帖子。我需要在循環中做一些額外的解析嗎? – metersk

+0

我剛剛編輯它。現在應該沒問題。 – aIKid

+0

真棒,謝謝! – metersk

1

您正在創建一個字典,只需要爲每次迭代循環覆蓋一個鍵。也許列表更有意義?

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

逐行打印,這似乎可行,但它沒有格式化URL,因爲它們是由美麗的名字解析的。你的解決方案我得到aa ='3DYB7DL5S & b = 3DW7482 & c = 3D9YZVAZR & d = 3DEDMP3B & e = 3D1「'color」=「」href ='3D「http://elink.tiffany.com/r/YB7DL5S/32FU1/5A6EIF/QFMQOO/1XF33/52/h?='n:underline =「」style ='3D「text-decoratio ='>點擊這裏,其中orignallyally得到這個3D「http://elink.tiffany.com/r/YB7DL5S/32FU1/5A6EIF/QFMQOO/BRLMA/52/h」= – metersk