2016-12-25 57 views
2

我試圖從this page颳起價格。BeautifulSoup:表找不到

我需要這個表:

table class = "table table-condensed table-info" 

然而,當我打印的內容和搜索的表,它不能被發現:

from BeautifulSoup import BeautifulSoup 
import urllib2 
from bs4 import BeautifulSoup 

url = "https://www.predictit.org/Contract/4393/Will-Obama-pardon-Hillary-Clinton#openoffers" 

page = urllib2.urlopen(url).read() 
soup = BeautifulSoup(page) 
print soup 

任何幫助將不勝感激!

回答

1

實際的問題 - 價格會加載獨立的異步請求到不同的端點。您需要模擬在你的代碼:

from bs4 import BeautifulSoup 
import requests 

url = "https://www.predictit.org/Contract/4393/Will-Obama-pardon-Hillary-Clinton#openoffers" 
price_url = "https://www.predictit.org/PrivateData/GetPriceListAjax?contractId=4393" 

with requests.Session() as session: 
    session.headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36'} 
    session.get(url) # visit main page 

    # request prices 
    response = session.get(price_url) 
    soup = BeautifulSoup(response.content, "html.parser") 
    tables = soup.select("table.table-info") 
    for row in tables[0].select("tr")[2:]: 
     values = [td.find(text=True, recursive=False) for td in row('td') if td.text] 
     print(values) 

打印第一「是」表的內容(用於演示目的):

[u'13', u'1555', u'12', u'240'] 
[u'14', u'707', u'11', u'2419'] 
[u'15', u'2109', u'10', u'3911'] 
[u'16', u'1079', u'9', u'2634'] 
[u'17', u'760', u'8', u'2596'] 
[u'18', u'510', u'7', u'970'] 
[u'19', u'973', u'6', u'1543'] 
[u'20', u'483', u'5', u'2151'] 
[u'21', u'884', u'4', u'1195'] 
[u'22', u'701', u'3', u'950'] 

注意通過,我們維持網絡的刮會議requests.Session()這裏。

另請注意,price_url包含contractId GET參數 - 如果您要請求具有價格的其他頁面,請務必使用相應的contractId

+0

此功能完美。謝謝!! – gtownrower

0

可以使用BeautifulSoup的select功能來查找CSS選擇器的元素:

>>> soup = BeautifulSoup(page) 
>>> soup.select('table.table.table-condensed.table-info') 

[<table class="table table-condensed table-striped table-info"> 
<tbody> 
<tr> 
<td>Symbol:</td> 
<td>CLINTON.OBAMAPARDON</td> 
</tr> 
<tr> 
<td>Start Date:</td> 
<td>11/10/2016</td> 
</tr> 
<tr> 
<td>End Date:</td> 
<td>01/20/2017 11:59 PM (ET)</td> 
</tr> 
<tr> 
<td>Shares Traded:</td> 
<td>388,522</td> 
</tr> 
<tr> 
<td>Today's Volume:</td> 
<td>3,610</td> 
</tr> 
<tr> 
<td>Total Shares:</td> 
<td>143,774</td> 
</tr> 
<tr> 
<td>Today's Change:</td> 
<td style="color: green">+1<span style="font-family: helvetica;">¢</span> <i class="glyphicons up_arrow green" style="margin-top: 3px;"></i></td> 
</tr> 
</tbody> 
</table>] 
+0

謝謝。尋找類「table table-condensed table-info」的不同表格。它不顯示使用此代碼 – gtownrower

+0

這會返回「表格表 - 精簡表格 - 表格 - 表格 - 信息」表格 – gtownrower