2017-08-05 72 views
1

我有下面的html表格,想要獲取表格數據,即存在於第一行表格中的「收入($ M)$ 135,987」。如何使用python beautifulsoup來實現這一點。使用python 3.6獲取html表格行數據美麗的湯

<table data-reactid=".romjx8c48.1.0.5.1:1.4.0.3.1.0.0.0.0.1.0.0.0.0"> 
<thead data-reactid=".romjx8c48.1.0.5.1:1.4.0.3.1.0.0.0.0.1.0.0.0.0.0"> 
    <tr data-reactid=".romjx8c48.1.0.5.1:1.4.0.3.1.0.0.0.0.1.0.0.0.0.0.0"> 
    <th data-reactid=".romjx8c48.1.0.5.1:1.4.0.3.1.0.0.0.0.1.0.0.0.0.0.0.0" width="200"> 
    </th> 
    <th data-reactid=".romjx8c48.1.0.5.1:1.4.0.3.1.0.0.0.0.1.0.0.0.0.0.0.1:$th-$ millions"> 
    $ millions 
    </th> 
    <th data-reactid=".romjx8c48.1.0.5.1:1.4.0.3.1.0.0.0.0.1.0.0.0.0.0.0.1:$th-% change"> 
    % change 
    </th> 
    </tr> 
</thead> 
<tbody data-reactid=".romjx8c48.1.0.5.1:1.4.0.3.1.0.0.0.0.1.0.0.0.0.1"> 
    <tr data-reactid=".romjx8c48.1.0.5.1:1.4.0.3.1.0.0.0.0.1.0.0.0.0.1.$company-data-Revenues ($M)"> 
    <td class="title" data-reactid=".romjx8c48.1.0.5.1:1.4.0.3.1.0.0.0.0.1.0.0.0.0.1.$company-data-Revenues ($M).0"> 
    Revenues ($M) 
    </td> 
    <td data-reactid=".romjx8c48.1.0.5.1:1.4.0.3.1.0.0.0.0.1.0.0.0.0.1.$company-data-Revenues ($M).1"> 
    $135,987 
    </td> 
    <td data-reactid=".romjx8c48.1.0.5.1:1.4.0.3.1.0.0.0.0.1.0.0.0.0.1.$company-data-Revenues ($M).2"> 
    27.1% 
    </td> 
    </tr> 

腳本直接從源提取數據:

import requests 
from bs4 import BeautifulSoup as bs 

r = requests.get('http://fortune.com/fortune500/amazon-com/') 
soup = bs(r.content, 'html.parser') 

result = soup.find('div', {'class': 'small-12 columns'}) 
table = result.find_all('table')[0] # Grab the first table 
print(table.find('td', {'data-reactid': '.romjx8c48.1.0.5.1:1.4.0.3.1.0.0.0.0.1.0.0.0.0.1.$company-data-Revenues ($M).1'}).text) 

回答

1

選擇 '數據reactid' 具有值」 .romjx8c48.1.0.5.1:1.4.0.3.1.0.0.0.0.1。 0.0.0.0.1。$ company-data-Revenues($ M).1'}並讀取它的文本。

from bs4 import BeautifulSoup 

html = """<table data-reactid=".romjx8c48.1.0.5.1:1.4.0.3.1.0.0.0.0.1.0.0.0.0"> 
    <thead data-reactid=".romjx8c48.1.0.5.1:1.4.0.3.1.0.0.0.0.1.0.0.0.0.0"> 
     <tr data-reactid=".romjx8c48.1.0.5.1:1.4.0.3.1.0.0.0.0.1.0.0.0.0.0.0"> 
     <th data-reactid=".romjx8c48.1.0.5.1:1.4.0.3.1.0.0.0.0.1.0.0.0.0.0.0.0" width="200"> 
     </th> 
     <th data-reactid=".romjx8c48.1.0.5.1:1.4.0.3.1.0.0.0.0.1.0.0.0.0.0.0.1:$th-$ millions"> 
     $ millions 
     </th> 
     <th data-reactid=".romjx8c48.1.0.5.1:1.4.0.3.1.0.0.0.0.1.0.0.0.0.0.0.1:$th-% change"> 
     % change 
     </th> 
     </tr> 
    </thead> 
    <tbody data-reactid=".romjx8c48.1.0.5.1:1.4.0.3.1.0.0.0.0.1.0.0.0.0.1"> 
     <tr data-reactid=".romjx8c48.1.0.5.1:1.4.0.3.1.0.0.0.0.1.0.0.0.0.1.$company-data-Revenues ($M)"> 
     <td class="title" data-reactid=".romjx8c48.1.0.5.1:1.4.0.3.1.0.0.0.0.1.0.0.0.0.1.$company-data-Revenues ($M).0"> 
     Revenues ($M) 
     </td> 
     <td data-reactid=".romjx8c48.1.0.5.1:1.4.0.3.1.0.0.0.0.1.0.0.0.0.1.$company-data-Revenues ($M).1"> 
     $135,987 
     </td> 
     <td data-reactid=".romjx8c48.1.0.5.1:1.4.0.3.1.0.0.0.0.1.0.0.0.0.1.$company-data-Revenues ($M).2"> 
     27.1% 
     </td> 
     </tr> 
     <tr data-reactid=".romjx8c48.1.0.5.1:1.4.0.3.1.0.0.0.0.1.0.0.0.0.1.$company-data-Profits ($M)"> 
     <td class="title" data-reactid=".romjx8c48.1.0.5.1:1.4.0.3.1.0.0.0.0.1.0.0.0.0.1.$company-data-Profits ($M).0"> 
     Profits ($M) 
     </td> 
     <td data-reactid=".romjx8c48.1.0.5.1:1.4.0.3.1.0.0.0.0.1.0.0.0.0.1.$company-data-Profits ($M).1"> 
     $2,371.0 
     </td> 
     <td data-reactid=".romjx8c48.1.0.5.1:1.4.0.3.1.0.0.0.0.1.0.0.0.0.1.$company-data-Profits ($M).2"> 
     297.8% 
     </td> 
     </tr> 
     </tbody> 
    </table> 
    """ 

soup = BeautifulSoup(html, 'html.parser') 
print(soup.find('td', {'data-reactid': '.romjx8c48.1.0.5.1:1.4.0.3.1.0.0.0.0.1.0.0.0.0.1.$company-data-Revenues ($M).1'}).text) 

輸出:

$135,987 

更新響應評論:

呈現頁面的JavaScript,你可以使用Selenium以使其:

首先安裝硒:

sudo pip3 install selenium 

然後獲取驅動程序https://sites.google.com/a/chromium.org/chromedriver/downloads如果您使用的是Windows或Mac,則可以使用無鍍鉻「Chrome Canary」無頭版本。

import bs4 as bs 
from selenium import webdriver 

browser = webdriver.Chrome() 

url = "http://fortune.com/fortune500/amazon-com/" 
browser.get(url) 
html_source = browser.page_source 
browser.quit() 
soup = bs.BeautifulSoup(html_source, "html.parser") 
# print (soup) 
tds = soup.find_all('td') 
print(tds[1].text) 

或者從事其他非硒方法見我的回答Scraping Google Finance (BeautifulSoup)

+0

哇超級....其實我想獲取來自http://fortune.com/fortune500/amazon-com所有有用的信息/,我嘗試了一些腳本,添加到查詢中,請檢查它給出的錯誤「AttributeError:'NoneType'對象沒有屬性'文本'」 –

+1

我更新了答案以迴應您的評論。 –

+0

謝謝,我會對此工作...... –