2017-09-14 71 views
1

我嘗試從我的base_url頁面(在圖片中標記爲藍色圓圈)刮掉特定的行。頁面源代碼在另一張圖片中。用BeautifulSoup颳去特定的表格行

我的目標是讓那些< td>標籤,但不知何故,我不能讓我的代碼與他們。

我的代碼:

from bs4 import BeautifulSoup 
from selenium import webdriver 
import requests, csv, re, pandas, numpy 

base_url = "http://www.basket.fi/sarjat/ottelu/?game_id=3502579&season_id=93783&league_id=4+"+"#mbt:2-400$t&0=1" 
browser = webdriver.PhantomJS() 
browser.get(base_url) 
table = BeautifulSoup(browser.page_source, 'lxml') 

for data in table.find_all("tr",{"class":"row2"}): 
    print(data.find("td").text) 

enter image description here

enter image description here

+0

什麼問題,請解釋一下? –

+0

@TarunLalwani我的代碼沒有得到我想要的< td >標籤。 –

+0

可能獲取pastebin或原始網址中的HTML嗎? –

回答

1

通常你可以選擇通過屬性的HTML元素,但此文檔的「類」屬性是不是非常有幫助,因爲有許多其他'tr'標籤在同一個班級。

在這種情況下,您可以使用列表索引來選擇標籤。

for td in table.find_all("tr", {"class":"row2"})[25].find_all('td')[1:]: 
    print(td.get_text(strip=True)) 
+0

非常感謝! :) –