我有一个任务来证明,当您继续单击第一个链接时,大多数Wikipedia页面都会导致出现“哲学”页面。
我创建了一个代码,该代码使用xpath找到了第一个链接,但是问题是我应该忽略放在括号内的链接。
例如,在文本中(粗体链接):Semiosis(源自希腊语:?????????,s?meí?sis,动词??????,s?meiô,“ to标记”)是任何形式的活动 ...
该div中的第一个链接是“希腊语”,但这会使我陷入循环,因此我想过滤掉该链接,然后单击括号后的第一个链接。在这种情况下,“活动”。
有没有办法忽略括号中的链接?
start_page = "https://en.wikipedia.org/wiki/Special:Random"
def click_link():
link = driver.find_element_by_xpath("//div[@class='mw-parser-output']/p/a")
link.click()
driver.get(start_page)
redirects = 0
title = driver.find_element_by_tag_name("title").text
while title != "Philosophy":
click_link()
redirects += 1
title = driver.find_element_by_tag_name("title").text
print(redirects)
Run Code Online (Sandbox Code Playgroud)
这是您的代码。使用以下兄弟姐妹来获取下一个子项目。
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium import webdriver
def click_link():
wait=WebDriverWait(driver,5)
element = wait.until(expected_conditions.element_to_be_clickable((By.XPATH, "//div[@class='mw-parser-output']/p/a[1]/following-sibling::a")))
element.click()
driver = webdriver.Chrome()
driver.get("https://en.wikipedia.org/wiki/Special:Random")
title=driver.title
redirects=0
while title != "Action (philosophy) - Wikipedia":
click_link()
redirects += 1
title =driver.title
print(title)
print(redirects)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
614 次 |
| 最近记录: |