从ResultSet对象beautifulsoup称号 [英] beautifulsoup title from resultset object

查看:320
本文介绍了从ResultSet对象beautifulsoup称号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在这一段时间,但似乎无法得到使用beautifulsoup ResultSet对象的元素的文本值。这里是失败的方法:

I have been at this for a while, but can't seem to get the text value of an element of a resultset object using beautifulsoup. Here is the method that is failing:

def __getNameOfProduct(self, product):
    #product is of type bs4.resultset...

    for value in product:
        print value.find_all("div",class_="proddisc").title.string

其自己的标记,所以我没有一个网址(我通过教程的工作),但这里是我得到的错误

Its my own markup so I don't have a url (I am working through a tutorial), but here is the error I am getting

  Traceback (most recent call last):
  File "ctd.py", line 64, in <module>
    main()
  File "ctd.py", line 60, in main
    p.getItemsInStock()
  File "ctd.py", line 26, in getItemsInStock
    return self.__returnItemDetailAsDictionary(itemDetail)
  File "ctd.py", line 32, in __returnItemDetailAsDictionary
    nameOfProduct = self.__getNameOfProduct(product)
  File "ctd.py", line 44, in __getNameOfProduct
    print value.find_all("div",class_="proddisc").title.string
  AttributeError: 'ResultSet' object has no attribute 'title'

任何帮助将非常AP preciated。

Any help would be very much appreciated.

谢谢!

推荐答案

在访问属性的方式仅适用于单一的对象不是many_objects为你find_all获得。

the way you accessing attribute is only valid for single object not for many_objects as you getting with "find_all".

我收到了你的要求,这将工作:

as i got your requirement, this will work:

html = urllib.urlopen("http://yoursite.com")
soup = BeautifulSoup(html) 
prodisc_div = soup.findAll('div', attrs={class:"prodisc"})
for each in prodisc_div:
  print each.get("title")

这篇关于从ResultSet对象beautifulsoup称号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆