2011-12-15 14 views
3

最近、別のユーザーがWebテーブルExtracting information from a webpage with pythonから情報を抽出する質問をしたことが分かりました。 ekhumoroからの回答は、他のユーザーが尋ねたページでうまくいきます。下記参照。Pythonとlxmlでテーブルからテキストを抽出する

from urllib2 import urlopen 
from lxml import etree 

url = 'http://www.uscho.com/standings/division-i-men/2011-2012/' 

tree = etree.HTML(urlopen(url).read()) 

for section in tree.xpath('//section[starts-with(@id, "section_")]'): 
    print section.xpath('h3[1]/text()')[0] 
    for row in section.xpath('table/tbody/tr'): 
     cols = row.xpath('td//text()') 
     print ' ', cols[0].ljust(25), ' '.join(cols[1:]) 
    print 

私の問題は、このページhttp://www.uscho.com/rankings/d-i-mens-poll/ を解析するためのガイドとして、このコードを使用しています。以下の変更を使用すると、h1とh3だけを印刷できます。テーブルの

入力

url = 'http://www.uscho.com/rankings/d-i-mens-poll/' 
tree = etree.HTML(urlopen(url).read()) 

for section in tree.xpath('//section[starts-with(@id, "rankings")]'): 
    print section.xpath('h1[1]/text()')[0] 
    print section.xpath('h3[1]/text()')[0] 
    for row in section.xpath('table/tbody/tr'): 
     cols = row.xpath('td/b/text()') 
     print ' ', cols[0].ljust(25), ' '.join(cols[1:]) 
    print 

出力

USCHO.com Division I Men's Poll 
December 12, 2011 

構造が同じであるように思わので、私は似たコードを使用することはできません理由として途方に暮れてよ。私は頭の中でただ機械的なエンジニアです。どんな助けもありがとうございます。

答えて

4

lxmlは素晴らしいですが、あなたはxpathに慣れていない場合、私はあなたにBeautifulSoupをお勧めします。

from urllib2 import urlopen 
from BeautifulSoup import BeautifulSoup 

url = 'http://www.uscho.com/rankings/d-i-mens-poll/' 
soup = BeautifulSoup(urlopen(url).read()) 

section = soup.find('section', id='rankings') 
h1 = section.find('h1') 
print h1.text 
h3 = section.find('h3') 
print h3.text 
print 

rows = section.find('table').findAll('tr')[1:-1] 
for row in rows: 
    columns = [data.text for data in row.findAll('td')[1:]] 
    print '{0:20} {1:4} {2:>6} {3:>4}'.format(*columns) 

このスクリプトの出力は次のようになります。

USCHO.com Division I Men's Poll 
December 12, 2011 

Minnesota-Duluth  (49) 12-3-3 999 
Minnesota     14-5-1 901 
Boston College   12-6-0 875 
Ohio State   (1) 13-4-1 848 
Merrimack     10-2-2 844 
Notre Dame    11-6-3 667 
Colorado College   9-5-0 650 
Western Michigan   9-4-5 647 
Boston University   10-5-1 581 
Ferris State    11-6-1 521 
Union      8-3-5 510 
Colgate     11-4-2 495 
Cornell     7-3-1 347 
Denver      7-6-3 329 
Michigan State   10-6-2 306 
Lake Superior    11-7-2 258 
Massachusetts-Lowell  10-5-0 251 
North Dakota    9-8-1 88 
Yale      6-5-1 69 
Michigan     9-8-3 62 
+0

ありがとう!私は前に美しいスープを聞いていなかった。あまりにも簡単です。 – drivendaily

0

'table/tr''table/tbody/tr'を交換してください。

+0

私がそれをしたとき、私は他のものをたくさん持っていました。私は問題のデータを除いて多くの表形式のデータを得ました。私はなぜそれが幸せだと思うが、それはちょっと困惑している! – ThinkCode

2

表の構造はわずかに異なり、空白の項目がある列があります。

可能lxmlソリューション:

from urllib2 import urlopen 
from lxml import etree 

url = 'http://www.uscho.com/rankings/d-i-mens-poll/' 
tree = etree.HTML(urlopen(url).read()) 

for section in tree.xpath('//section[@id="rankings"]'): 
    print section.xpath('h1[1]/text()')[0], 
    print section.xpath('h3[1]/text()')[0] 
    print 
    for row in section.xpath('table/tr[@class="even" or @class="odd"]'): 
     print '%-3s %-20s %10s %10s %10s %10s' % tuple(
      ''.join(col.xpath('.//text()')) for col in row.xpath('td')) 
    print 

出力:

USCHO.com Division I Men's Poll December 12, 2011 

1 Minnesota-Duluth   (49)  12-3-3  999   1 
2 Minnesota       14-5-1  901   2 
3 Boston College      12-6-0  875   3 
4 Ohio State     (1)  13-4-1  848   4 
5 Merrimack       10-2-2  844   5 
6 Notre Dame       11-6-3  667   7 
7 Colorado College      9-5-0  650   6 
8 Western Michigan      9-4-5  647   8 
9 Boston University     10-5-1  581   11 
10 Ferris State      11-6-1  521   9 
11 Union        8-3-5  510   10 
12 Colgate        11-4-2  495   12 
13 Cornell        7-3-1  347   16 
14 Denver        7-6-3  329   13 
15 Michigan State      10-6-2  306   14 
16 Lake Superior      11-7-2  258   15 
17 Massachusetts-Lowell    10-5-0  251   18 
18 North Dakota       9-8-1   88   19 
19 Yale         6-5-1   69   17 
20 Michigan        9-8-3   62   NR 
関連する問題