Py学习  »  Python

为什么一个python数组只在元素被更改时向另一个数组追加一个指针?[复制品]

Jesse Reza Khorasanee • 5 年前 • 1502 次点击  

我想要所有的 iframe 从网页上。

代码:

site = "http://" + url
f = urllib2.urlopen(site)
web_content =  f.read()

soup = BeautifulSoup(web_content)
info = {}
content = []
for iframe in soup.find_all('iframe'):
    info['src'] = iframe.get('src')
    info['height'] = iframe.get('height')
    info['width'] = iframe.get('width')
    content.append(info)
    print(info)       

pprint(content)

结果 print(info) :

{'src': u'abc.com', 'width': u'0', 'height': u'0'}
{'src': u'xyz.com', 'width': u'0', 'height': u'0'}
{'src': u'http://www.detik.com', 'width': u'1000', 'height': u'600'}

结果 pprint(content) :

[{'height': u'600', 'src': u'http://www.detik.com', 'width': u'1000'},
{'height': u'600', 'src': u'http://www.detik.com', 'width': u'1000'},
{'height': u'600', 'src': u'http://www.detik.com', 'width': u'1000'}]

为什么内容的价值不对?当我 打印(信息) .

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40815
 
1502 次点击  
文章 [ 4 ]  |  最新文章 5 年前