简体   繁体   English

如何使用Selenium和python从网页获取特殊单词的链接?

[英]How to get link with special word from webpage using selenium and python?

I am working on a python script which aim is to get all links containing a certain word from a given webpage. 我正在研究一个python脚本,目的是从给定的网页中获取包含某个单词的所有链接。

www.example.com/about/someone

www.example.com/profile/someone

www.example.com/about/someone2

So the code should get only those links which contains the word about . 因此,代码应仅获取包含about的单词的那些链接。 I am getting the following error: 我收到以下错误:

"AttributeError: 'WebElement' object has no attribute 'get' "

And here is my code: 这是我的代码:

driver.get("http://example.com/mypage")
time.sleep(20)

links = driver.find_elements_by_tag_name("a")
for link in links:
  if '/about/' in link.get( 'href' ):
     print (link.get_attribute("href"))
driver.get("http://example.com/mypage")
driver.implicitly_wait(20)

links = driver.find_elements_by_tag_name("a")
for link in links:
  href = link.get_attribute('href')

  if not href:
      continue

  if '/about/' in href:
     print(href)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM