我想了解这个失败的原因,以及我应该在我的脚本中修改什么来解决这个问题,并能够提供所需的抓取。遵循以下数据:
试验地点:
http://sports.williamhill.com/bet/pt/betlive/24
我正在尝试收集这些框中的链接:
find_element_by_xpath
是正确的,但是当我尝试运行脚本时,它返回错误:
selenium.common.exceptions.WebDriverException: Message: TypeError: node.ownerDocument is null
虚拟工作室代码中的我的脚本项目:
import time
import requests
import pandas as pd
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import json
url = "http://sports.williamhill.com/bet/pt/betlive/24"
option = Options()
option.headless = True
driver = webdriver.Firefox(options=option)
driver.get(url)
time.sleep(1)
element = driver.find_element_by_xpath("//tr[@class='rowLive']//a/@href")
html_content = element.get_attribute('outerHTML')
soup = BeautifulSoup(html_content, 'html.parser')
table = soup.find(name='href')
df_full = pd.read_html(str(table))[0]
importar = {}
importar['importando'] = df_full.to_dict('records')
print(importar['importando'])
driver.quit()
我猜您的错误在于您在
element = driver.find_element_by_xpath("//tr[@class='rowLive']//a/@href")
行中使用了find_element_by_xpath
而不是find_elements_by_xpath
,因此这实际上返回了一个元素,而您需要在那里获取所有链接。所以目前
html_content
是一个单一元素的HTML。对于
soup
和table
容器也是如此相关问题 更多 >
编程相关推荐