[英]Assigning the output of a for loop to a variable
I am trying to assign the output of a for loop to a variable or dictionary, but as of right now i can only get it to print the first iteration of the loop and it's not even in the right format. 我正在尝试将for循环的输出分配给变量或字典,但是截至目前,我只能获取它来打印循环的第一次迭代,甚至格式都不正确。
Here is my code: 这是我的代码:
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
I would like the format of the output of this to be how this code executes, but all this odes is print out the extracted links properly: 我希望此输出的格式是此代码的执行方式,但是所有这些颂歌都正确地打印出提取的链接:
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
Thanks! 谢谢!
EDIT: 编辑:
The way i would like it to print is this: 我希望它的打印方式是这样的:
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"=
with the solution below i ma getting this: 与下面的解决方案,我可以得到这个:
http://elink.tiffany.com/r/YB7DL5S/32FU1/5A6EIF/QFMQOO/1XF33/52/h?=' n:underline="" style='3D"text-decoratio='>click here, 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"text-decoration:none;' 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“ text-decoration:none;' unicode="">ENGAGEMENT,
unicode =“”> ENGAGEMENT,
You are creating a dict with just one key which you overwrite for each iteration of the loop. 您正在创建仅具有一个键的字典,该键对于循环的每次迭代都将被覆盖。 Maybe a list makes more sense?
也许列表更有意义?
urls = []
for x in soup.find_all('a', href=True):
urls.append(x)
You keep assigning to a same key in the dict, that's your only mistake: 您在字典中一直分配相同的键,这是您唯一的错误:
Collect and put it into a list: 收集并放入列表中:
urls=[]
for x in soup.find_all('a', href=True):
urls.append(x['href'])
print urls
Or into a dictionary: 或放入字典中:
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
This should work as you want it, hope this helps! 这应该可以按您希望的方式工作,希望能有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.