首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用beautifulsoup4从coingecko中抓取表内容

使用beautifulsoup4从coingecko中抓取表内容
EN

Stack Overflow用户
提问于 2022-05-16 07:44:39
回答 1查看 97关注 0票数 0

这是我通过引用这里获得的代码/输出:scraping coins market with BeautifulSoup。但是,我不能让这段代码运行:

代码语言:javascript
复制
url = 'https://www.coingecko.com/en/coins/ethereum'
soup = BeautifulSoup(url.content, "html.parser")

data = []
for tr in soup.select('tr[class]'):
    if 'sponsored' in tr['class']:
       continue
    _, Exchange, Pair, Price, Spread, +2%Depth, -2%Depth, 24hVolume, Volume%, LastTraded, TrustScore,  *_ = tr.select('td')

    data.append( (Exchange.get_text(strip=True),
                  Pair.get_text(strip=True),
                  Price.get_text(strip=True),
                  Spread.get_text(strip=True),
                  +2%Depth.get_text(strip=True),
                  -2%Depth.get_text(strip=True),
                  24hVolume.get_text(strip=True),
                  Volume%.get_text(strip=True),
                  LastTraded.get_text(strip=True),
                  TrustScore.get_text(strip=True),
              ) )

这是我得到的错误:

代码语言:javascript
复制
     """File "C:\Users\Computer\PycharmProjects\new\gecko.py", line 22
      _, Exchange, Pair, Price, Spread, +2%Depth, -2%Depth, 24hVolume, Volume%, LastTraded,TrustScore,  *_ = tr.select('td')
                                                              ^
     SyntaxError: invalid syntax

编辑:

我根据第一个答案编辑了代码,没有发现任何错误,但只有一个空白方括号出现。以下是编辑的代码:

代码语言:javascript
复制
    _, exchange, pair, price, spread, depth0, depth1, volume0, volume1, lasttraded, trustscore,  *_ = tr.select('td')

    data.append( (exchange.get_text(strip=True),
              pair.get_text(strip=True),
              price.get_text(strip=True),
              spread.get_text(strip=True),
              depth0.get_text(strip=True),
              depth1.get_text(strip=True),
              volume0.get_text(strip=True),
              volume1.get_text(strip=True),
              lasttraded.get_text(strip=True),
              trustscore.get_text(strip=True),
              ) )
print(data)
EN

回答 1

Stack Overflow用户

发布于 2022-05-16 08:24:55

检查您的变量名,这里有一些规则用于:

character

  • cannot

  • 必须以字母或下划线开头,以数字开头,

  • 只能包含α-数字字符和下划线(A、0-9和_)

  • or

  • or

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

https://stackoverflow.com/questions/72255716

复制
相关文章

相似问题

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