2017-02-27 110 views
4

我試圖從一個頁面結果使用BeautifulSoup來獲得:BeautifulSoup find_all限於50個結果?

req_url = 'http://www.xscores.com/soccer/livescores/25-02' 
request = requests.get(req_url) 
content = request.content 
soup = BeautifulSoup(content, "html.parser") 
scores = soup.find_all('tr', {'style': 'height:18px;'}, limit=None) 
print(len(scores)) 
>50 

我讀這之前的解決方案:Beautiful Soup findAll doen't find them all ,我試圖html.parser,LXML和html5lib,但他們沒有返回超過50結果。有什麼建議麼?

謝謝使用css-selector查詢您

回答

1

嘗試。

scores = soup.select('#scoretable > tr[style*="height:18px;"]') 
print(len(scores)) 

>>>613 
+0

完美,謝謝! – StevenH

2

試試這個 -

req_url = 'http://www.xscores.com/soccer/livescores/25-02' 
request = requests.get(req_url) 
html=request.text 
soup = BeautifulSoup(html, "html5lib") 
scoretable=soup.find('tbody',id='scoretable') 
scores=scoretable.find_all('tr') 
len(scores) 
>617 
1

此行只發現行「family:宋體;樣式。

scores = soup.find_all('tr', {'style': 'height:18px;'}, limit=None) 

如果您查看頁面源並搜索"height:18px;",您會看到50個匹配項。但是,如果您搜索不帶引號的height:18px;,則會看到613個匹配項。

您需要編輯該行才能找到行 height:18px;樣式(和其他值)。 您可以根據documentations傳遞一個正則表達式的樣式參數,也許是這樣的:

soup.find_all('tr', style = re.compile('height:18px'), limit=None) 
相關問題