我想从这个网站表中抓取数据: https: //www.oddsportal.com/moving-margins/ 这是我使用的代码:它返回嵌套列表,将它们作为 csv 文件中的行,但结果我get 不准确,尤其是当一场比赛有多个 tr[@class="odd"] 时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 executionWebDriverWait(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 oddfor 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)for t in table: print(t)问题是:有些比赛有多个 tr[@class="odd"],我无法将每场比赛与其特定的相关联tr[@class="dark"]没有类的 tr 中还有另一个数据始终位于 tr="odd" 之前(contain th[@class="first2"]),我们可以添加此数据吗?我们可以添加博彩公司名称(最后的数据tr="odd")?
添加回答
举报
0/150
提交
取消