2017-06-18 142 views
0

我「米試圖用seleniumpython檢索詞‘年度報告’和‘首次公開發行招股說明書’。蟒蛇硒打印「日」

我嘗試使用driver.find_elements_by_class_name('sic_highlight')但因爲有多個表是具有相同class_name,它從其他表打印一切爲好。

如何我剛打印出的「年度報告」和「IPO募資」的文字,而無需通過其他表搜索?

<table class="sic_table" cellspacing="1"> 
    <thead> 
    <tr class="sic_tableTopRow"> 
     <th scope="col">Report Type</th> 
     <th scope="col">Year Ended</th> 
     <th scope="col">Download</th> 
    </tr> 
    </thead> 
    <tbody> 
     <tr class="sic_highlight"> 
     <th colspan="3" scope="col" class="sic_highlight">Annual Report</th> 
     </tr> 
     <tr> 
      <th class="si_left">Annual Report&nbsp;2016</th> 
      <td class="si_center">Jun 2016</td> 
      <td class="si_center"> 
       <a href="some_link">Part 1(1.41 MB)</a><br> 
      </td> 
     .... 
     .... 
     </tr> 
     <tr class="sic_highlight"> 
     <th colspan="3" scope="col" class="sic_highlight">IPO Prospectus</th> 
     </tr> 
     <tr> 
      <th class="si_left">IPO Prospectus&nbsp;2011</th> 
      <td class="si_center">Jul 2011</td> 
      <td class="si_center"> 
       <a href="some_link">Part 1(5.10 MB)</a><br> 
      </td> 
     </tr> 
    </tbody> 
</table> 
+0

如果沒有看到其他表*,很難給出一個很好的答案。你能分享一個嗎? – Andersson

回答

0

請使用以下XPath

//table[@class='sic_table']/tbody/tr/th 
0

此XPath能夠在烏拉圭回合的HTML定位包括文本code.Try出這個

XPATH: - *//tr[@class="sic_highlight"]/th[contains(text(),"Annual Report"|"IPO Prospectus")]

driver.find_element_by_xpath('*//tr[@class="sic_highlight"]/th[contains(text(),"Annual Report"|"IPO Prospectus")]) 
0

你說有多個表在頁面上。你知道這張桌子的完整路徑嗎?獲取每個'th'元素的全部(a.k.a.絕對)路徑,並將單獨的WebDriver調用到find_element_by_xpath。

現在已經說了,你通常不希望使用絕對路徑來定位元素(它們需要很長時間,而且很脆弱)。所以,如果有可能(即您或您認識已經開發了這個網頁,並能完全控制的HTML的人),你應該把一個ID在該表中,然後你可以這樣做:

driver.find_element_by_id('tableIdHere').find_elements_by_class_name('sic_highlight'); 

甚至更​​好,把ID放在你想要的兩個'th'元素上。