2014-11-21 29 views
0

在此網頁上有一個「顯示學習位置」選項卡,當我單擊該選項卡時,它會顯示整個位置列表並更改我包含在此程序中的網址。當我運行程序來打印出整個位置列表時,我得到這樣的結果:Python:讀取隱藏的HTML表格的內容

soup = BeautifulSoup(urllib2.urlopen('https://clinicaltrials.gov/ct2/show/study/NCT01718158?term=NCT01718158&rank=1&show_locs=Y#locn').read()) 

for row in soup('table')[5].findAll('tr'): 
    tds = row('td') 
    if len(tds)<2: 
     continue 
    print tds[0].string, tds[1].string #, '\n'.join(filter(unicode.strip, tds[1].strings)) 

Local Institution None 
Local Institution None 
Local Institution None 
Local Institution None 
Local Institution None 

等等.....剩下的信息就出來了。我覺得我在這裏失去了一些東西。我的結果應該是:

United States, California 
Va Long Beach Healthcare System 
Long Beach, California, United States, 90822 
United States, Georgia 
Gastrointestinal Specialists Of Georgia Pc 
Marietta, Georgia, United States, 30060 
United States, New York 
Weill Cornell Medical College 

等等。我想打印出整個位置列表。

+0

它看起來像內容可以基於用戶代理進行修改或者可能由JavaScript填充。 'wget --no-check-certificate https://clinicaltrials.gov/ct2/show/study/NCT01718158?term=NCT01718158&rank=1&s how_locs = Y'給我一個沒有任何位置的文件,重新尋找。 – Tom 2014-11-21 15:14:27

回答

0

當地的研究機構只有一個表格單元格,但你正在跳過這些。

也許你需要提取的所有單元格中的數據,只跳過行,而不<td>細胞這裏:

for row in soup('table')[5].findAll('tr'): 
    tds = row('td') 
    if not tds: 
     continue 
    print u' '.join([cell.string for cell in tds if cell.string]) 

這將產生

United States, California 
Va Long Beach Healthcare System 
Long Beach, California, United States, 90822 
United States, Georgia 
Gastrointestinal Specialists Of Georgia Pc 
Marietta, Georgia, United States, 30060 
# .... 
Local Institution 
Taipei, Taiwan, 100 
Local Institution 
Taoyuan, Taiwan, 333 
United Kingdom 
Local Institution 
London, Greater London, United Kingdom, SE5 9RS 
+0

感謝萬Martijn。太感謝了。有效! – 2014-11-24 15:11:26