【发布时间】:2022-01-07 18:22:15
【问题描述】:
使用 pandas read_html() 时无法正确获取 行 格式。我正在寻找对方法本身或底层 html(通过 bs4 抓取)进行调整以获得所需的输出。
当前输出:
(注意它是 1 行包含两种类型的数据。理想情况下应该分为 2 行,如下所示)
期望:
复制问题的代码:
import requests
import pandas as pd
from bs4 import BeautifulSoup # alternatively
url = "http://ufcstats.com/fight-details/bb15c0a2911043bd"
df = pd.read_html(url)[-1] # last table
df.columns = [str(i) for i in range(len(df.columns))]
# to get the html via bs4
headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Max-Age": "3600",
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0",
}
req = requests.get(url, headers)
soup = BeautifulSoup(req.content, "html.parser")
table_html = soup.find_all("table", {"class": "b-fight-details__table"})[-1]
【问题讨论】:
标签: python pandas web-scraping beautifulsoup html-table