【问题标题】:How to find broken links in Selenium + Python如何在 Selenium + Python 中查找损坏的链接
【发布时间】:2018-11-04 17:51:07
【问题描述】:

我正在尝试在 Selenium 和 Python 中查找损坏的链接,但在代码中出现错误:

import requests
from selenium import webdriver

chrome_driver_path = "D:\\drivers\\chromedriver.exe"

driver=webdriver.Chrome(chrome_driver_path)

driver.get('https://google.co.in/')
links = driver.find_elements_by_css_selector("a")
images = driver.find_elements_by_css_selector("img")
for link in links:
    r = requests.head(link.get_attribute('href')
    print(r.status_code == 200)

无法在页面上找到损坏的链接是否有其他解决方案?

获取:

raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(主机='myaccount.google.com',端口=443):最大 url 超出重试次数:/?utm_source=OGB&utm_medium=app(由 SSLError(SSLEOFError(8, 'EOF 发生违反协议 (_ssl.c:777)'),))

在处理上述异常的过程中,又发生了一个异常:

self._sslobj.do_handshake() ssl.SSLEOFError: EOF 发生违规 协议 (_ssl.c:777)

在处理上述异常的过程中,又发生了一个异常:

Traceback(最近一次调用最后一次):

【问题讨论】:

  • 您能否添加您得到的错误,并将代码示例放入文本而不是使用屏幕截图

标签: python-3.x selenium selenium-webdriver


【解决方案1】:

因为你在下一行缺少右括号还是错字?

r = requests.head(link.get_attribute('href'))

【讨论】:

    【解决方案2】:

    要查找页面上链接的状态,您可以使用以下解决方案:

    • 代码块:

      import requests
      from selenium import webdriver
      
      options = webdriver.ChromeOptions() 
      options.add_argument("start-maximized")
      options.add_argument('disable-infobars')
      driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
      driver.get('https://google.co.in/')
      links = driver.find_elements_by_css_selector("a")
      for link in links:
          r = requests.head(link.get_attribute('href'))
          print(link.get_attribute('href'), r.status_code)
      
    • 控制台输出:

      https://mail.google.com/mail/?tab=wm 302
      https://www.google.co.in/imghp?hl=en&tab=wi 200
      https://www.google.co.in/intl/en/options/ 301
      https://myaccount.google.com/?utm_source=OGB&utm_medium=app 302
      https://www.google.co.in/webhp?tab=ww 200
      https://maps.google.co.in/maps?hl=en&tab=wl 302
      https://www.youtube.com/?gl=IN 200
      https://play.google.com/?hl=en&tab=w8 302
      https://news.google.co.in/nwshp?hl=en&tab=wn 301
      https://mail.google.com/mail/?tab=wm 302
      https://www.google.com/contacts/?hl=en&tab=wC 302
      https://drive.google.com/?tab=wo 302
      https://www.google.com/calendar?tab=wc 302
      https://plus.google.com/?gpsrc=ogpy0&tab=wX 302
      https://translate.google.co.in/?hl=en&tab=wT 200
      https://photos.google.com/?tab=wq&pageId=none 302
      https://www.google.co.in/intl/en/options/ 301
      https://docs.google.com/document/?usp=docs_alc 302
      https://books.google.co.in/bkshp?hl=en&tab=wp 200
      https://www.blogger.com/?tab=wj 405
      https://hangouts.google.com/ 302
      https://keep.google.com/ 302
      https://earth.google.com/web/ 200
      https://www.google.co.in/intl/en/options/ 301
      https://accounts.google.com/ServiceLogin?hl=en&passive=true&continue=https://www.google.co.in/ 200
      https://www.google.co.in/webhp?hl=en&sa=X&ved=0ahUKEwj0qNPqnqHbAhXYdn0KHXpeAo0QPAgD 200
      

    【讨论】:

      【解决方案3】:
      from selenium import webdriver
      chrome_driver_path = "D:\\drivers\\chromedriver.exe"
      driver=webdriver.Chrome(chrome_driver_path)
      import requests
      for link in links:
          r = requests.head(link)
          if r.status_code!=404:
               driver.get(link)
          else:
                print(str(link) + " isn't available.")
      

      【讨论】:

      • 对我来说看起来像不完整的代码,我们将从那里获得链接和链接的值。请更正
      猜你喜欢
      • 2019-01-24
      • 1970-01-01
      • 2011-08-26
      • 1970-01-01
      • 2015-07-21
      • 2014-04-10
      • 1970-01-01
      • 2016-05-02
      • 1970-01-01
      相关资源
      最近更新 更多