【问题标题】:getting error "selenium.common.exceptions.InvalidSessionIdException: Message: invalid session id" while crawling links of a table抓取表的链接时出现错误“selenium.common.exceptions.InvalidSessionIdException:消息:无效会话 id”
【发布时间】:2019-09-16 09:30:18
【问题描述】:

试图抓取 table-tr-td[2] 中给出的链接,但面临以下错误 --selenium.common.exceptions.InvalidSessionIdException: Message: invalid session id。

我尝试过使用 selenium webdriver,但没有得到 seesion 错误的问题。

    from bs4 import BeautifulSoup
    from selenium import webdriver

    url="https://www.zaubacorp.com/company-list"

    driver = webdriver.Chrome(r'C:\chromedriver.exe')
    driver.get(url)

    driver.close()

    soup = BeautifulSoup(driver.page_source,'html.parser')
    table = soup.find('table',{'id':'table'})
    body = table.find('tbody')
    for links in body.find_all('a'):
        print(links['href'])

请帮我解决这个问题。提前谢谢。

【问题讨论】:

    标签: python-3.x beautifulsoup selenium-chromedriver


    【解决方案1】:

    在获取 page_source 值之前,您是关闭浏览器,因此 selenium 无法获取会话。立即尝试。

    from bs4 import BeautifulSoup
    from selenium import webdriver
    
    url="https://www.zaubacorp.com/company-list"
    driver = webdriver.Chrome(r'C:\chromedriver.exe')
    driver.get(url)
    soup = BeautifulSoup(driver.page_source,'html.parser')
    driver.close()
    table = soup.find('table',{'id':'table'})
    body = table.find('tbody')
    for links in body.find_all('a'):
     print(links['href'])
    

    【讨论】:

      猜你喜欢
      • 2015-09-07
      • 2016-06-08
      • 2022-12-04
      • 1970-01-01
      • 2019-09-23
      • 1970-01-01
      • 2018-11-16
      • 2021-04-09
      • 1970-01-01
      相关资源
      最近更新 更多