Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从oddsportal上刮下运动桌_Python_Selenium_Web Scraping - Fatal编程技术网

Python 从oddsportal上刮下运动桌

Python 从oddsportal上刮下运动桌,python,selenium,web-scraping,Python,Selenium,Web Scraping,我试着去刮这个网页 但是代码有时有效,有时无效,即使有效,也无法获取每次匹配所需的所有数据 u = 'https://www.oddsportal.com/moving-margins/' driver = webdriver.Chrome(executable_path=r"C:\chromedriver.exe") driver.maximize_window() driver.get(u) #Use Explicit time wait for fast executi

我试着去刮这个网页
但是代码有时有效,有时无效,即使有效,也无法获取每次匹配所需的所有数据

u = 'https://www.oddsportal.com/moving-margins/'
driver = webdriver.Chrome(executable_path=r"C:\chromedriver.exe")
driver.maximize_window()
driver.get(u)
#Use Explicit time wait for fast execution
WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#moving_margins_content_overall")))
driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
table_data =  driver.find_elements_by_xpath("//div[@id='moving_margins_content_overall']//tr[@class='odd' or @class='dark']")
table =[]
# Creating a list of lists, where each list consist all data in each row either with class dark or odd

for data in table_data:
    row = []
    dark_row = data.find_elements_by_xpath((".//th//a"))
    for col in dark_row:
        row.append(col.text.replace("\n"," "))
    odd_row = data.find_elements_by_xpath((".//following-sibling::tr[@class='odd']//td"))
    for col in odd_row:
        row.append(col.text.replace("\n", " "))
    table.append(row)
我的目标是使用以下列将数据存储到csv文件中:

sport    country    competiton    handicap    match_date  match                  hdp_open  hdp_close  bookmaker
Tennis   Czech      Ostrava..     AH 0 Games  Today12:00  Karatsev A. - Otte O.  0.5       -1.5      Nordicbet   


我认为代码中的问题是,在某些情况下,页面中有一个“暗”行来表示许多“赔率”行。因此,当您循环元素时,您将为一个实际上有更多记录的表创建一条记录

此代码应该适合您的需要,但请记住,它不是最佳的,因为它不考虑可能的异常,但它是一个起点:

从selenium导入webdriver
从selenium.webdriver.support.wait导入WebDriverWait
将selenium.webdriver.support.expected_条件导入为EC
从selenium.webdriver.common.by导入
u='https://www.oddsportal.com/moving-margins/'
driver=webdriver.Chrome(可执行文件路径=r“chromedriver.exe”)
驱动程序。最大化_窗口()
驱动程序。获取(u)
#使用显式时间等待快速执行
WebDriverWait(驱动程序,30)。直到(例如,元素的存在位置((By.CSS)选择器,“#移动页边距(整体内容)”))
执行_脚本(“window.scrollTo(0,document.body.scrollHeight)”)
tables=driver.find\u elements\u by\u xpath(//div[@id='moving\u margins\u content\u total']//table)
tableData=[]
对于表中的表:
trDark=table.通过xpath('.//tr[@class=“dark”]”查找元素
trOdds=table.find_elements_by_xpath('.//tr[@class=“odd”]'))
行=[trDark.text.strip().replace(“\n”,”)]
对于奇数输入TRODS:
tds=[
td.text.strip().replace(“\n”,”)
对于奇数形式的td。通过xpath(“.//td”)查找元素
]
行=行+tds
tableData.append(行)
打印(表格数据)

“代码有时有效,有时无效”并不能解释问题所在,您能更准确地解释一下吗?你有什么错误吗?有时返回一个错误:selenium.common.exceptions.NoSuchElementException:Message:没有这样的元素:找不到元素:{“method”:“xpath”,“selector”:“//following sibling::tr//th[@class='first2']”尝试复制完整的xpath并运行代码。即使我更改了xpath,刮取的数据并非表中的所有数据