得到格属性VAL和DIV正文 [英] get div attribute val and div text body
本文介绍了得到格属性VAL和DIV正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是小code得到DIV ATTR值。所有div名称相同与相同ATTR名。
Here is small code to get div attr value. All div name are same with same attr name.
redditFile = urllib2.urlopen("http://www.bing.com/videos?q=owl")
redditHtml = redditFile.read()
redditFile.close()
soup = BeautifulSoup(redditHtml)
productDivs = soup.findAll('div', attrs={'class' : 'dg_u'})
for div in productDivs:
print div.find('div', {"class":"vthumb"})['smturl']
#print div.find("div", {"class":"tl text-body"}) This print none rather then div text
第一次印刷给出了一些网址(有时4,6,8等),然后
first print gives some urls(some times 4,6,8 etc) and then
KeyError Traceback (most recent call last)
<ipython-input-34-cc950a8a84f7> in <module>()
26 productDivs = soup.findAll('div', attrs={'class' : 'dg_u'})
27 for div in productDivs:
---> 28 print div.find('div', {"class":"vthumb"})['smturl']
29 print div.find("div", {"class":"tl text-body"})
/usr/local/lib/python2.7/dist-packages/bs4/element.pyc in __getitem__(self, key)
903 """tag[key] returns the value of the 'key' attribute for the tag,
904 and throws an exception if it's not there."""
--> 905 return self.attrs[key]
906
907 def __iter__(self):
KeyError: 'smturl'
所有div名称都一样有相同的 smturl
ATTR的名字,为什么它给 KeyError异常
任何帮助吗?
as all div name are same with same smturl
attr name, why it gives KeyError
any help?
推荐答案
并非所有的div有 smturl
属性,因此您需要将属性添加到找到
电话。也没有在 productDivs
所有元素包含你正在寻找的div,所以我添加了测试,如果找到
返回无。
Not all divs have smturl
attribute, so you need to add the attribute to find
call. Also not all elements in productDivs
contain divs you are looking for, hence I've added test if find
returns None.
In [27]: for div in productDivs:
....: if div.find('div', {"class":"vthumb", 'smturl': True}) is not None:
....: print div.find('div', {"class":"vthumb", 'smturl': True})['smturl']
....:
http://ts2.mm.bing.net/th?id=OMB.9hfZ6cCDfUWbpw&pid=2.1
http://ts4.mm.bing.net/th?id=OMB1.n%2b12M8SoyFcsag&pid=2.1
http://ts4.mm.bing.net/th?id=OMB.ev1wnIiszGjhUg&pid=2.1
http://ts4.mm.bing.net/th?id=OMB.hDLa5PO07Chclw&pid=2.1
http://ts2.mm.bing.net/th?id=OMB.xDT9H25QFJ2jBw&pid=2.1
http://ts3.mm.bing.net/th?id=OMB.BULQolkxkaZ0uw&pid=2.1
http://ts3.mm.bing.net/th?id=OMB.xp3c0DyKrfmB7Q&pid=2.1
http://ts4.mm.bing.net/th?id=OMB.MxP9fUyaJCRyhw&pid=2.1
http://ts4.mm.bing.net/th?id=OMB2.CWjPPKiJQc4z6w&pid=2.1
http://ts1.mm.bing.net/th?id=OMB1.ZVKhvML3%2bPzM1w&pid=2.1
http://ts1.mm.bing.net/th?id=OMB.SLn%2b0NwKeUdZXw&pid=2.1
http://ts2.mm.bing.net/th?id=OMB.4HJqrT9pBevGlg&pid=2.1
http://ts2.mm.bing.net/th?id=OMB2.HgWYR9sjPw6JlQ&pid=2.1
http://ts1.mm.bing.net/th?id=OMB.RyBXWQ9sH9wThw&pid=2.1
http://ts2.mm.bing.net/th?id=OMB2.Vf21EgXRXMcdfg&pid=2.1
http://ts3.mm.bing.net/th?id=OMB2.BIb6qwbHniC1vw&pid=2.1
http://ts3.mm.bing.net/th?id=OMB1.H9bwRYncKU380A&pid=2.1
http://ts2.mm.bing.net/th?id=OM1.mBXeu55OD4VimQ&pid=2.1
这篇关于得到格属性VAL和DIV正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文