首页 > 解决方案 > Python bs4突然'TypeError:'NoneType'对象不可迭代'

问题描述

为什么我的代码在几次成功循环后突然遇到错误?

我的代码:

import requests
from bs4 import BeautifulSoup
import pandas as pd
import os

path =r'C:\Users\mrfau\Documents\python\stock_screener_001\crypto_project'
urls= pd.read_csv(os.path.join(path,r'all_coin_link.csv'))
urls_2 = ['https://coinmarketcap.com/currencies/bitcoin/', 'https://coinmarketcap.com/currencies/ethereum/', 'https://coinmarketcap.com/currencies/solana/']

ranks = []
names = []
prices = []
count = 0

for url in urls['coin_link']:
    r = requests.get(url)
    soup = BeautifulSoup(r.text, 'html.parser')
    count += 1
    print('Loop', count, f'started for {url}')
    rank = []
    name = []
    price = []
    for item in soup.find('div', class_ = 'sc-16r8icm-0 bILTHz'):
        item = item.text
        rank.append(item)
    ranks.append(rank)

    for ticker in soup.find('h2', class_ = 'sc-1q9q90x-0 jCInrl h1'):
        ticker = ticker.text
        name.append(ticker)
    names.append(name)

    for price_tag in soup.find('div', class_ = 'sc-16r8icm-0 kjciSH priceTitle'):
        price_tag = price_tag.text
        price.append(price_tag)
    prices.append(price)

df = pd.DataFrame(ranks)
df2 = pd.DataFrame(names)
df3 = pd.DataFrame(prices)
final_table = pd.concat([df, df2, df3], axis=1)
final_table.columns=['rank', 'type', 'watchlist', 'name', 'symbol', 'price', 'changes']

final_table.to_csv(os.path.join(path,r'summary.csv'))

我尝试使用 urls_2 时的成功输出:

      rank  type                watchlist      name symbol       price changes
0  Rank #1  Coin  On 2,701,732 watchlists   Bitcoin    BTC  $64,166.71   0.37%
1  Rank #2  Coin  On 2,187,875 watchlists  Ethereum    ETH   $4,673.75   0.76%
2  Rank #5  Coin    On 890,655 watchlists    Solana    SOL     $229.95   1.41%
      rank  type                watchlist      name symbol       price changes
0  Rank #1  Coin  On 2,701,732 watchlists   Bitcoin    BTC  $64,166.71   0.37%
2  Rank #5  Coin    On 890,655 watchlists    Solana    SOL     $229.95   1.41%

当我尝试运行完整列表(7k URL)时,循环在循环编号处出错。76,当我检查链接是 https://coinmarketcap.com/currencies/kucoin-token/

Loop 74 started for https://coinmarketcap.com/currencies/compound/
Loop 75 started for https://coinmarketcap.com/currencies/nexo/
Traceback (most recent call last):
  File "c:\Users\mrfau\Documents\python\stock_screener_001\crypto_project\get_watchlist.py", line 23, in <module>
    for item in soup.find('div', class_ = 'sc-16r8icm-0 bILTHz'):
TypeError: 'NoneType' object is not iterable

知道为什么成功后突然出现错误吗?我检查了类它都在那里。

我希望有一个人可以帮助我。谢谢

标签: pythonweb-scrapingbeautifulsoup

解决方案


推荐阅读