You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用urllib与Pandas抓取HTML表格转CSV时遇TypeError错误求助

解决Pandas读取HTML表格转CSV时的TypeError问题

Hey there, let's break down why you're running into this TypeError and fix it up quickly.

你的问题场景

你尝试抓取Yahoo Finance的BTC-JPY历史数据表格并保存为CSV,但代码报错:

代码如下:

import urllib, pandas as pd
url = 'https://finance.yahoo.com/quote/BTC-JPY/history?period1=1314403200&period2=1314489600&interval=1d&filter=history&frequency=1d'
fo = 'test.txt'
response = urllib.request.urlopen(url)
html = response.read()
data = pd.read_html(html)
data.to_csv(fo, index = False, header=False, sep=',', mode='w')

错误信息:TypeError: ufunc 'add' did not contain a loop with signature matching...

问题根源

The core issue here is that pd.read_html() doesn't return a single DataFrame—it returns a list of DataFrames (since an HTML page can have multiple tables). When you try to call .to_csv() directly on this list, pandas can't process it, hence the confusing type error.

修正后的代码

Here's the working version with explanations:

import urllib.request
import pandas as pd

# Clean up the URL (replace & with actual &)
url = 'https://finance.yahoo.com/quote/BTC-JPY/history?period1=1314403200&period2=1314489600&interval=1d&filter=history&frequency=1d'
# Use .csv extension for proper CSV file
output_file = 'btc_jpy_history.csv'

# Fetch the HTML content
response = urllib.request.urlopen(url)
html = response.read()

# Extract tables from HTML (returns a list)
tables = pd.read_html(html)

# Check if we found any tables, then use the first one (Yahoo's history table is first)
if tables:
    target_df = tables[0]
    # Save to CSV - keep headers since they're useful for the data
    target_df.to_csv(output_file, index=False, header=True, sep=',', mode='w')
    print(f"Data saved successfully to {output_file}!")
else:
    print("No tables found in the fetched HTML.")

额外提示

  • I switched the output file to .csv instead of .txt—this makes the file recognizable as a CSV for other tools.
  • Added a check for empty tables to avoid another error if Yahoo's page structure changes.
  • Fixed the URL entities (& → &) for cleaner, more reliable parsing.

内容的提问来源于stack exchange,提问作者Roman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:56:04