这是从表中下载数据并在cmd上输出的代码。我想知道是否可以在行和列的相同结构表中下载相同的数据? 这就是我的尝试。
代码:
import urllib
import re
from urlparse import urlparse
from bs4 import BeautifulSoup as bs
urls = ["http://physics.iitd.ac.in/content/list-faculty-members", "http://www.iitkgp.ac.in/commdir3/list.php?division=3&deptcode=ME","http://www.iitkgp.ac.in/commdir3/list.php?division=3&deptcode=CE"]
i = 0
while i< len(urls):
htmlfile = urllib.urlopen(urls[i])
htmltext = htmlfile.read()
soup = bs(htmltext)
tables = soup.find_all('table', attrs = {'border': '0' , 'width' : '100%' , 'cellpadding': '10'})
head = soup.find_all('h2' , attrs = {'class' : 'title style3'})
ree = tables.find_all('tr')
hea = head.find_all('big').find_all('strong')
datasets = []
q = []
s = []
t = hea.get_text()
q.append(t)
for b in ree:
x = [td.get_text() for td in b.find_all('td')]
dataset = [strong.get_text() for strong in b.find('td').find('a').find_all('strong')]
datasets.append(dataset)
q.append(x)
print q
i+=1
答案 0 :(得分:1)
我认为在使用表格数据时,很多人会建议使用pandas库。对于结构良好的HTML,您可以盲目地使用pandas read_html。
import pandas as pd
tables = pd.read_html("http://physics.iitd.ac.in/content/list-faculty-members")
dataframe = tables[0]