繁体   English   中英

如何从 html 内部的 div 标签中获取某个表标签?

[英]How to get a certain table tag from div tag inside of html?

我正在尝试从网站http://www.o1vsk.lv/index.php/stundu-izmainas获取表格信息。 html 我需要提取的网页内容

from bs4 import BeautifulSoup
from urllib.request import urlopen
html = urlopen("http://www.o1vsk.lv/index.php/stundu-izmainas").read()

rows=[]
soup=BeautifulSoup(html,"html.parser")
box = soup.find('div', {'class': 'DRight'})

该程序获取页面的所有内容,而我只需要一个文本格式的小表格,例如:


  1. ...
  2. ...
  3. ...
  4. ...
  5. ...
  6. ...
  7. ...
  8. ...

抱歉,由于我的声誉 < 50,我还不能发表评论

这是我给你的解决方案。

  1. 找到所有table标签,它将返回 HTML 代码
table = box.findAll("table")
  1. 将 HTML 转换为 Pandas DataFrame (df)。 为什么索引 = 1? 因为你想要的表在索引 1
df = pd.read_html(str(table))[1]
  1. 最后,删除Unnamed的列以仅获取所需的列
df.loc[:, ~df.columns.str.match('Unnamed')]

这是完整的代码:

from pandas import pd
from bs4 import BeautifulSoup
from urllib.request import urlopen
html = urlopen("http://www.o1vsk.lv/index.php/stundu-izmainas").read()

rows=[]
soup=BeautifulSoup(html,"html.parser")
box = soup.find('div', {'class': 'DRight'})

table = box.findAll("table")
df = pd.read_html(str(table))[1]

df.loc[:, ~df.columns.str.match('Unnamed')]

如果这对您有帮助,请点赞:) 谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM