簡體   English   中英

簡單:如何使用 Selenium Webdriver 分離由 class 名稱抓取的元素?

[英]EASY: How do I separate elements grabbed by class name using Selenium Webdriver?

我從 links.txt 打開兩個鏈接並輸出到 names.txt。

我試圖擺脫這個詞:“FEATURING”並在元素之間添加:“,”。

當前 output:

FEATURING
Arietta AdamsIsiah MaxwellFEATURING
Vanessa VegaRichard Mann

所需的 output:

Arietta Adams, Isiah Maxwell
Vanessa Vega, Richard Mann

我的代碼:

one = open("links.txt", "r")

for two in one.readlines():
  driver.get(two)
  sleep(3)
  for element in driver.find_elements_by_class_name('sceneColActors'):
    with open("names.txt", "a") as testtxt:
      testtxt.write(element.text)

Html:

<div class="sceneCol sceneColActors"> == $0
   <b>Featuring </b>
   <a href="/en/Arietta-Adams/58224" title="Arietta Adams">Arietta Adams</a>
   <span class="actorSeparator">, </span>
   <a href="/en/Isiah-Maxwell/34204" title="Isiah Maxwell">Isiah Maxwell</a>

這是 html 的屏幕截圖,以防萬一。

我想您應該嘗試a獲取標簽。 嘗試

driver.find_elements_by_css_selector('.sceneColActors a')

代替

driver.find_elements_by_class_name('sceneColActors')

現在您可以單獨獲取所有需要的單詞,只需在它們之間添加 delimiter 即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM