【问题标题】:Get text from <span class: with Beautifulsoup and requests从 <span 类中获取文本:使用 Beautifulsoup 和请求
【发布时间】:2024-01-16 14:00:01
【问题描述】:

所以我试图从网站获取特定文本,但它只给我错误 (floor = soup.find('span', {'class': 'text-white fs-14px text-truncate attribute-value '})。文本 AttributeError: 'NoneType' 对象没有属性 'text')

我特别想获得“底价”文本。

我的代码:

import bs4
from bs4 import BeautifulSoup

#target url
url = "https://magiceden.io/marketplace/solsamo"
#act like browser
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}

response = requests.get('https://magiceden.io/marketplace/solsamo')
#parse the downloaded page
soup = BeautifulSoup(response.content, 'lxml')

floor = soup.find('span', {'class': 'text-white fs-14px text-truncate attribute-value'}).text

print(floor)

【问题讨论】:

    标签: python html beautifulsoup python-requests request


    【解决方案1】:

    您收到的 HTML 中不需要数据:

    response = requests.get('https://magiceden.io/marketplace/solsamo')
    

    您可以通过查看页面源代码来确定这一点:

    view-source:https://magiceden.io/marketplace/solsamo
    

    您应该使用Selenium 而不是requests 来获取您的数据,或者您可以在此页面上检查XHR 请求,也许您可​​以通过其他链接使用requests 获取此数据。

    【讨论】: