[英]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'})
該程序獲取頁面的所有內容,而我只需要一個文本格式的小表格,例如:
抱歉,由於我的聲譽 < 50,我還不能發表評論
這是我給你的解決方案。
table
標簽,它將返回 HTML 代碼table = box.findAll("table")
df = pd.read_html(str(table))[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.