提取没有类或id的表

2024-06-17 08:17:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从http://marine-transportation.capitallink.com/indices/baltic_exchange_history.html?ticker=BDI刮一张桌子

虽然这看起来相当容易,但我不可能以这样一种方式来识别表,以至于我无法提取数据。有谁能帮我做正确的鉴定吗

import urllib3 
import urllib.request
from bs4 import BeautifulSoup
import pandas as pd
import requests
import csv
import re


url = 'http://marine-transportation.capitallink.com/indices/baltic_exchange_history.html?ticker=BDI'
r = requests.get(url)
soup = BeautifulSoup(r.text, 'lxml')
col = row.find_all('td')
column_1 = col[0].string.strip()

#
date = []
closing_rate = []

#Here i need a reference to the correct table
table = soup.find()

for row in table.find_all('tr')[1:]:
    col = row.find_all('td')

    column_1 = col[0].string.strip()
    date.append(column_1)
    column_2 = col[1].string.strip()
    closing_rate.append(column_2)



columns = {'date': date, 'closing_rate': ClosingRate}
df = pd.DataFrame(columns)

df.to_csv('Baltic_Dry.csv')

Tags: csvimporthttpdatestringratetablecolumn
1条回答
网友
1楼 · 发布于 2024-06-17 08:17:00

可以使用唯一的样式属性来标识所需的表。在

例如,在这个页面here,它看起来包含索引数据的表是550px宽的。您可以使用:

soup.findAll('table', width="550")

请注意:我不得不使用同一网站上的另一个页面,因为您发布的页面需要登录。希望页面结构类似。

相关问题 更多 >