gpt4 book ai didi

python - 使用 beautifulsoup 获取文本。

转载 作者:行者123 更新时间:2023-12-01 09:05:02 26 4
gpt4 key购买 nike

我在使用 beautifulsoup 剥离 html 并只保留文本时遇到问题。当我运行它时,我收到错误 AttributeError: ResultSet object has no attribute 'get_text'。您可能将项目列表视为单个项目。当您打算调用 find() 时,您是否调用了 find_all()? 当我像我一样在 divs 变量中 findAll 时,是否可以获取文本有它?

我的代码:

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)

最佳答案

试试这个:

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 列表

关于python - 使用 beautifulsoup 获取文本。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52133866/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com