2016-12-01 11 views
1

私はURLの配列をループし、企業のリストから掲示板のメンバーを掻き回そうとしています。下の私のループでは、配列内の最初の要素だけを実行して結果を複製するという問題があるようです。これについての助けに感謝します。コード:「HTML」はあなたがBeautifulSoup(html, "html.parser") を使用するときにHTMLが割り当てられた後だけループに入れては存在しない用量のでURLの配列をループしている美しいスープ

from bs4 import BeautifulSoup 
import requests 

#array of URLs to loop through, will be larger once I get the loop working correctly 
tickers = ['http://www.reuters.com/finance/stocks/companyOfficers?symbol=AAPL.O', 'http://www.reuters.com/finance/stocks/companyOfficers?symbol=GOOG.O'] 

board_members = [] 
output = [] 
soup = BeautifulSoup(html, "html.parser") 

for t in tickers: 
    html = requests.get(t).text 
    officer_table = soup.find('table', {"class" : "dataTable"}) 
    for row in officer_table.find_all('tr'): 
     cols = row.find_all('td') 
     if len(cols) == 4: 
      board_members.append((t, cols[0].text.strip(), cols[1].text.strip(), cols[2].text.strip(), cols[3].text.strip())) 

     for t, name, age, year_joined, position in board_members: 
      output.append(('{} {:35} {} {} {}'.format(t, name, age, year_joined, position))) 
+0

このコードは実行しないでください。 'BeautifulSoup(html'は' html'が定義されていないというエラーになります) –

+0

とにかく、シンボルだけをリストに格納することを検討してください。 –

答えて

1
soup = BeautifulSoup(html, "html.parser") 

for t in tickers: 
    html = requests.get(t).text 
    officer_table = soup.find('table', {"class" : "dataTable"}) 

forループの外にスープを入れ、これには、エラーが発生します。

for t in tickers: 
    html = requests.get(t).text 
    soup = BeautifulSoup(html, "html.parser") 
    officer_table = soup.find('table', {"class" : "dataTable"}) 
関連する問題