使用beautifulsoup获取文本。

时间:2018-09-02 04:01:16

标签: python beautifulsoup

我在使用beautifulsoup剥离html仅包含文本时遇到了问题。当我运行它时,出现错误AttributeError: ResultSet object has no attribute 'get_text'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?,就像我拥有findAll变量中的divs一样,有什么办法得到文本吗?

我的代码:

import requests
from bs4 import BeautifulSoup
url = 'https://www.brightscope.com/form-5500/basic-info/107299/Orthopedic-Institute-Of-Pennsylvania/15801790/Orthopedic-Institute-Of-Pennsylvania-401k-Profit-Sharing-Plan/'
page = requests.get(url)
soup = BeautifulSoup(page.text, 'html.parser')
divs = soup.findAll('span', class_='float-right').get_text()

for each in divs:
    print(each)

1 个答案:

答案 0 :(得分:1)

尝试一下:

import requests
from bs4 import BeautifulSoup
url = 'https://www.brightscope.com/form-5500/basic-info/107299/Orthopedic-Institute-Of-Pennsylvania/15801790/Orthopedic-Institute-Of-Pennsylvania-401k-Profit-Sharing-Plan/'
page = requests.get(url)
soup = BeautifulSoup(page.text, 'html.parser')
divs = soup.findAll('span', class_='float-right') #not on the collection of elements

for each in divs:
    print(each.get_text()) #get_text goes here on the element

编辑:

import requests
from bs4 import BeautifulSoup
url = 'https://www.brightscope.com/form-5500/basic-info/107299/Orthopedic-Institute-Of-Pennsylvania/15801790/Orthopedic-Institute-Of-Pennsylvania-401k-Profit-Sharing-Plan/'
page = requests.get(url)
soup = BeautifulSoup(page.text, 'html.parser')
divs = [e.get_text() for e in soup.findAll('span', class_='float-right')]

将为您提供字符串格式的div列表