Python 获取'list'对象在BS4中没有属性错误

Python 获取'list'对象在BS4中没有属性错误,python,beautifulsoup,bs4,Python,Beautifulsoup,Bs4,我是bs4新手,正在尝试为加密货币构建一个价格机器人。这是我目前掌握的代码: import requests import bs4 import csv from datetime import datetime def remove_all_whitespace(x): try: x = x.replace(" ", "") except: pass return x def trim_the_ends(x): try:

我是bs4新手,正在尝试为加密货币构建一个价格机器人。这是我目前掌握的代码:

import requests
import bs4
import csv
from datetime import datetime

def remove_all_whitespace(x):
    try:
        x = x.replace(" ", "")
    except:
        pass
    return x

def trim_the_ends(x):
    try:
        x = x.strip(' \t\n\r')
    except:
        pass
    return x

def remove_unneeded_chars(x):
    try:
        x = x.replace("$", "").replace("RRP", "")
    except:
        pass
    return x

URL = ("https://coinmarketcap.com/assets/golem-network-tokens/")

response = requests.get(URL)

soup = bs4.BeautifulSoup(response.text)

price = soup.select("span#quote_price.text-large").get_text()

print (price)
但我得到了这个错误:

AttributeError: 'list' object has no attribute 'get_text'
我做错了什么?据我所知.select不适用于列表项,但我如何提取列表

是,soup.select返回匹配项列表;选择器可以匹配0次或更多次

如果只想检索一个匹配项,请使用soup.select\u one方法返回第一个匹配项,如果没有匹配项,则使用None方法:

price = soup.select_one("span#quote_price.text-large").get_text()
但是,您加载的页面不包含该信息。该页面使用Javascript通过AJAX加载数据。请求不是浏览器,不会加载外部资源或执行Javascript代码

页面从中加载价格,改为加载:

>>> import requests
>>> r = requests.get('https://graphs.coinmarketcap.com/currencies/golem-network-tokens/')
>>> data = r.json()
>>> data['price_usd'][-1][1]
0.309104
该列表中每个条目的第一个元素是以微秒为单位的时间戳:

>>> from datetime import datetime
>>> datetime.fromtimestamp(data['price_usd'][-1][0] / 1000)
datetime.datetime(2017, 8, 29, 18, 34, 46)
但是,您可能应该使用他们的:

https://api.coinmarketcap.com/v1/ticker/golem-network-tokens/

select可能不接受列表参数,但它显然可以返回一个列表,我假设当多个项目满足请求条件时会返回句柄。谢谢您-现在我得到以下错误:AttributeError:'NoneType'对象没有属性'get_text';我假设这意味着它找不到任何东西。@sgerbhctim:我在回答中指定,如果没有匹配项,select_one将返回None。好的,您能告诉我CSS/HTML标记的错误在哪里吗?我觉得我做得好像是对的。@sgerbhctim:因此,请注意,您在浏览器中检查的DOM可能是执行Javascript代码改变对象树的结果。您需要查看页面的原始源代码,以了解可能会提供哪些请求。查看开发人员工具中的“网络”选项卡,查看是否有额外的请求加载您要提取的实际数据等@sgerbhctim:您必须在该站点上处理这些请求。