2014-02-21 58 views
0

我使用Python 3.3的工作同級車中第一個div標籤的內容獲取與本網站: http://www.nasdaq.com/markets/ipos/的Python webscraping和

我的目標是隻讀是在即將到來的IPO的公司。它在div標籤中,div class =「genTable thin floatL」這個類有兩個,目標數據是第一個。

這裏是我的代碼

from urllib.request import urlopen 
from bs4 import BeautifulSoup 
import re 

html = urlopen("http://www.nasdaq.com/markets/ipos/").read() 
soup = BeautifulSoup(html) 
for divparent in soup.find_all('div', attrs={'class':'genTable thin floatL'}) [0]: # I tried putting a [0] so it will only return divs in the first genTable thin floatL class 
    for div in soup.find_all('div', attrs={'class':'ipo-cell-height'}): 
     s = div.string 
     if re.match(r'\d{1,2}/\d{1,2}/\d{4}$', s): 
      div_next = div.find_next('div') 
      print('{} - {}'.format(s, div_next.string)) 

我想它僅返回

3/7/2014 - RECRO PHARMA, INC. 
2/28/2014 - VARONIS SYSTEMS INC 
2/27/2014 - LUMENIS LTD 
2/21/2014 - SUNDANCE ENERGY AUSTRALIA LTD 
2/21/2014 - SEMLER SCIENTIFIC, INC. 

但它打印所有的div類與re.match規範和多次爲好。我嘗試在divparent循環中插入[0]來只檢索第一個,但是這會導致重複問題。

編輯:這是根據warunsl解決方案更新的代碼。這工作。

from urllib.request import urlopen 
from bs4 import BeautifulSoup 
import re 

html = urlopen("http://www.nasdaq.com/markets/ipos/").read() 
soup = BeautifulSoup(html) 

divparent = soup.find_all('div', attrs={'class':'genTable thin floatL'})[0] 
table= divparent.find('table') 
for div in table.find_all('div', attrs={'class':'ipo-cell-height'}): 
     s = div.string 
     if re.match(r'\d{1,2}/\d{1,2}/\d{4}$', s): 
      div_next = div.find_next('div') 
      print('{} - {}'.format(s, div_next.string)) 

回答

1

您提到有兩個元素符合'class':'genTable thin floatL'標準。所以爲它的第一個元素運行for循環沒有任何意義。

因此,與

divparent = soup.find_all('div', attrs={'class':'genTable thin floatL'})[0] 

現在更換您的外部for循環你不必再做一個soup.find_all。這樣做將搜索整個文檔。您需要將搜索範圍限制爲divparent。所以,你這樣做:

table = divparent.find('table') 

代碼的其餘部分,提取日期和公司名稱是相同的,不同之處在於,他們將參照table變量。

for row in table.find_all('tr'): 
    for data in row.find_all('td'): 
     print data.string 

希望它有幫助。

+0

這工作得很好。謝謝! – user2859603