【问题标题】:Selenium webdriver with python throws requests.exceptions.MissingSchema [duplicate]带有python的Selenium webdriver抛出requests.exceptions.MissingSchema [重复]
【发布时间】:2020-01-30 08:05:14
【问题描述】:

运行以下脚本时出现以下错误:

requests.exceptions.MissingSchema:无效的 URL 'None':没有架构 提供。也许你的意思是http://None

我看到一个解决方案说通过 xpath 查找元素,但正如我所说的我是新人,我无法复制代码。

import requests
from selenium import webdriver
from selenium.webdriver.firefox.options import Options

option = webdriver.ChromeOptions()
option.add_argument("headless")

driver = webdriver.Chrome(options=option)
driver.get("https://charities.govt.nz/")
links = driver.find_elements_by_css_selector("a")
print("Number of links : %s" %len(links))

for link in links:
    r = requests.head(link.get_attribute('href'))
    print(link.get_attribute('href'), r.status_code)

我们将不胜感激。

【问题讨论】:

    标签: python selenium selenium-webdriver


    【解决方案1】:

    主要问题是由该网站上的无效链接引起的(即有一个空字符串链接)。我已经修改了您的代码以使用for-loop 中的if-statement 来检查链接是否以http 开头(因此它也会忽略该网站上的mailto 链接)。

    我还修改了检索和存储链接的方式。使用xpath 从网页中检索链接。链接存储在唯一的 strings 列表中,而不是 firefoxWebElements 列表中(即提高可用性并删除重复链接)。

    import requests
    from selenium import webdriver
    
    option = webdriver.ChromeOptions()
    option.add_argument("headless")
    
    driver = webdriver.Chrome(options=option)
    driver.get("https://charities.govt.nz/")
    
    # This variable contains all the links found
    all_links = [link.get_attribute("href") for link in driver.find_elements_by_xpath("//a[@href]")]
    
    # This variable contains all unique links (i.e. removes duplicates)
    unique_links = list(dict.fromkeys(all_links))
    
    print("Number of links : %s" %len(all_links))
    print("Number of unique links : %s" %len(unique_links))
    
    for link in unique_links:
        # If the link is easy to work with
        if link.startswith("http"):
            req = requests.head(link)
            print(link, req.status_code)
        else:
            print("Ignoring '{}'".format(link))
    
    

    【讨论】:

    • 有些人将我的问题标记为重复,但他们提供的解决方案不适用于我的场景。所以。谢谢@Tyler 的回复。在运行您的语句时,我收到“AttributeError:'FirefoxWebElement' 对象没有属性'startswith”你能建议一下吗?我对编程很陌生,我需要几个小时才能解决一个错误。
    • 对不起,我已经编辑了我的答案。这是我的懒惰,因为我使用link 作为string 数据类型,而它实际上是FirefoxWebElement。上面的代码现在通过link_url = link.get_attribute('href')href 存储在一个字符串中,允许您使用和操作link_url
    • 它仍然出现同样的错误。文件“C:\Users\dharg\PycharmProjects\BrokenLinks\venv\lib\site-packages\requests\models.py”,第 387 行,在 prepare_url 中引发 MissingSchema(error) requests.exceptions.MissingSchema: Invalid URL '': No提供的架构。也许你的意思是 http://?如何与您分享整个结果?
    • 有人在另一篇文章中提到了这一点“当通过 TAG_NAME 找到元素时,它向我显示了相同的错误,但对于 XPATH 它有效。提到的解决方案是:links = WebDriverWait(driver, 10).until( EC.visibility_of_any_elements_located((By.XPATH, "//div[@class='rc']//h3//ancestor::a[1]"))) 但在我的情况下如何使用它。再次感谢泰勒为您提供帮助
    • @G.Dhar,我再次更新了答案。真正的问题是页面上有一个带有空字符串的链接。我使用了一个简单的if-statement 来忽略这个和其他“难以处理”的链接,这样你就可以继续推进这个项目。我还更改了获取链接的方式以使用xpath,因此您可以看到它在您的场景中是如何工作的。
    猜你喜欢
    • 2020-12-21
    • 1970-01-01
    • 2016-11-13
    • 2017-07-17
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 2013-02-17
    • 2017-03-26
    相关资源
    最近更新 更多