BeautifulSoup get_text from find_all

孤街浪徒 提交于 2019-11-30 08:50:32

问题


This is my first work with web scraping. So far I am able to navigate and find the part of the HTML I want. I can print it as well. The problem is printing only the text, which will not work. I get following error, when trying it: AttributeError: 'ResultSet' object has no attribute 'get_text'

Here my code:

from bs4 import BeautifulSoup
import urllib

page = urllib.urlopen('some url')


soup = BeautifulSoup(page)
zeug = soup.find_all('div', attrs={'class': 'fm_linkeSpalte'}).get_text()


print zeug

回答1:


find_all() returns an array of elements. You should go through all of them and select that one you are need. And than call get_text()

UPD
For example:

    for el in soup.find_all('div', attrs={'class': 'fm_linkeSpalte'}):
        print el.get_text()

But note that you may have more than one element.




回答2:


I would close this issue for being a duplicate and link you to another I found that answers this question but I don't think I have the reputation needed to moderate... So...

Original Answer

Code for this:

for el in soup.findAll('div', attrs={'class': 'fm_linkeSpalte'}):
    print ''.join(el.findAll(text=True))

If a mod wants to close this question that would be helpful.



来源:https://stackoverflow.com/questions/21997587/beautifulsoup-get-text-from-find-all

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!