使用BeautifulSoup一个表中提取选定列 [英] Extracting selected columns from a table using BeautifulSoup

查看:827
本文介绍了使用BeautifulSoup一个表中提取选定列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图提取此数据表的第一列和第三列使用BeautifulSoup。从看HTML中的第一列有一个<第i 标记。感兴趣的其他列的< TD> 标记。在任何情况下,所有我已经能够走出与标签列的列表。但是,我只是想文本。

已经是一个列表,所以我不能使用的findAll(文= TRUE)。我不知道如何获得第一列的上市另一种形式。

 从BeautifulSoup进口BeautifulSoup
从SYS进口的argv
进口重文件名=的argv [1]#获取HTML文件作为一个字符串
html_doc =''。加入(开放(文件名,'R')。readlines方法())
汤= BeautifulSoup(html_doc)
表= soup.findAll('表')[0] .tbody.th.findAll('日')#系统​​相关的表是第一位的打印表


解决方案

您可以试试这个code:

 进口的urllib2
从BeautifulSoup进口BeautifulSoupURL =htt​​p://www.samhsa.gov/data/NSDUH/2k10State/NSDUHsae2010/NSDUHsaeAppC2010.htm
汤= BeautifulSoup(urllib2.urlopen(URL).read())在soup.findAll('表')[0] .tbody.findAll('TR')行:
    FIRST_COLUMN = row.findAll('日')[0] .contents
    third_column = row.findAll('TD')[2] .contents
    打印FIRST_COLUMN,third_column

正如你可以看到code只连接到URL,并得到了HTML和BeautifulSoup找到的第一个表,那么所有的TR,并选择第一列,这是'日'和第三列,这是一种TD

I am trying to extract the first and third columns of this data table using BeautifulSoup. From looking at the HTML the first column has a <th> tag. The other column of interest has as <td> tag. In any case, all I've been able to get out is a list of the column with the tags. But, I just want the text.

table is already a list so I can't use findAll(text=True). I'm not sure how to get the listing of the first column in another form.

from BeautifulSoup import BeautifulSoup
from sys import argv
import re

filename = argv[1] #get HTML file as a string
html_doc = ''.join(open(filename,'r').readlines())
soup = BeautifulSoup(html_doc)
table = soup.findAll('table')[0].tbody.th.findAll('th') #The relevant table is the first one

print table

解决方案

You can try this code:

import urllib2
from BeautifulSoup import BeautifulSoup

url = "http://www.samhsa.gov/data/NSDUH/2k10State/NSDUHsae2010/NSDUHsaeAppC2010.htm"
soup = BeautifulSoup(urllib2.urlopen(url).read())

for row in soup.findAll('table')[0].tbody.findAll('tr'):
    first_column = row.findAll('th')[0].contents
    third_column = row.findAll('td')[2].contents
    print first_column, third_column

As you can see the code just connects to the url and gets the html, and the BeautifulSoup finds the first table, then all the 'tr' and selects the first column, which is the 'th', and the third column, which is a 'td'.

这篇关于使用BeautifulSoup一个表中提取选定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆