首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用漂亮的汤来获取文本。

使用漂亮的汤来获取文本。
EN

Stack Overflow用户
提问于 2018-09-02 12:01:16
回答 1查看 175关注 0票数 1

我遇到了一些问题,我正在使用漂亮的汤剥离的html,只有文本。当我运行它的时候,我得到了错误,当我在我的divs变量中findAll的时候,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()?就在那里,只是为了得到文本,就像我有它一样?

我的代码:

代码语言:javascript
运行
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-02 12:05:41

试试这个:

代码语言:javascript
运行
复制
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

编辑:

代码语言:javascript
运行
复制
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列表

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52133866

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档