从<p>而不是<table>中的html表提取数据

2024-09-26 17:59:34 发布

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

我一直在使用pd.read\uhtml试图从一个url中提取数据,但是数据是列在

标记中的,而不是。我可能错过了一个简单的教训,但我不知道用什么函数来获得一个好的结果(一个表),而不是我得到的长字符串。如有任何建议,将不胜感激! 我使用了这两种方法,得到了相同的结果:

import requests import pandas as pd url ='http://www.linfo.org/acronym_list.html' dfs = pd.read_html(url, header =0) df = pd.concat(dfs) df

import pandas as pd
url ='http://www.linfo.org/acronym_list.html'
data = pd.read_html(url, header=0)
data[0]

输出[1]:

AMD高级微设备API应用程序编程接口ARP地址解析协议ARPANET高级研究项目代理网络作为自治系统ASCII美国信息交换标准码AT&;美国电话电报公司ATA先进技术附件ATM异步传输模式B字节BELUG Bellevue Linux用户组BGP边界网关协议


Tags: 数据orgimporthttpurlpandasreadhtml
1条回答
网友
1楼 · 发布于 2024-09-26 17:59:34

我用BeautifulSoup解析请求html中的每个标记p和br,最后的结果是一个数据帧…以后你可以把它导出到excel文件中…希望能对你有所帮助

from bs4 import BeautifulSoup
import requests
import pandas as pd

result = requests.get('http://www.linfo.org/acronym_list.html')
c = result.content
soup = BeautifulSoup(c, "html.parser")
samples = soup.find_all("p")

rows_list = []

for row in samples:
    tagstrong = row.find_all("strong")
    for x in tagstrong:
        #print(x.get_text())
        tagbr = row.find_all("br")
        for y in tagbr:
            new_row = {'letter':x.get_text(), 'content':y.next}
            rows_list.append(new_row)

df1 = pd.DataFrame(rows_list)
print(df1.head(10))

结果如下:

enter image description here

相关问题 更多 >

    热门问题