2013-01-05 45 views
3

做在Selenium Doc他們已經使用^$*以前的=運營商在下面的代碼:但他們沒有解釋爲什麼這樣特殊符號

soup.select('a[href="http://example.com/elsie"]') 
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>] 

soup.select('a[href^="http://example.com/"]') 
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, 
# <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, 
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>] 

soup.select('a[href$="tillie"]') 
# [<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>] 

soup.select('a[href*=".com/el"]') 
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>] 

回答

7

芹苴SE是substring matching attribute selectors adapted from CSS 3:只有

  • =比賽,如果給定值等於到元素的屬性值。
  • ^=僅在給定值爲時匹配該元素屬性值的前綴
  • $=僅在給定值爲時才匹配元素的屬性值的後綴
  • *=只有在元素的屬性值中包含的給定值爲時才匹配。

在你的情況:

  • a[href="http://example.com/elsie"]選擇任何a元素,其href屬性值等於http://example.com/elsie
  • a[href^="http://example.com/"]選擇任何a元素,其href屬性值以http://example.com/開頭。
  • a[href$="tillie"]選擇任何a元素,其href屬性值以tillie結尾。
  • a[href*=".com/el"]選擇任何a元素,其href屬性值包含.com/el
+0

該鏈接不在我的桌面上打開。你能告訴我在我的例子中它有什麼作用嗎? –

+0

哦!神開了!有了這個概念!感謝您的幫助:) –

+0

我可以對[Push data]有一些想法(http://stackoverflow.com/questions/14171223/push-the-excel-data-to-access-database-using-pyhton2-7 #comment19636110_14171223) –