Python 2.7 selenium webdriver 无法读取 table 站点上的 table 内容
Python 2.7 selenium webdriver failing to read table contents on java site
我一直在尝试对 java 网站进行网络抓取(对我来说是新网站),但每次我尝试从其主要 table 读取数据时,它都会失败。我知道我要搜索的元素在网站上,所以我不知道是什么导致找不到该元素。我可以搜索其他 ares,但由于某种原因我无法读取 table 数据(没有具有相同 class 名称的元素,但我已将该站点包含在下面的代码中)。有人可以帮助我了解我可能遗漏了什么吗?
注意:该站点需要 username/password 组合,我登录没问题。
我的代码如下:
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import sys, os, requests
from os import system
def main():
file = open("wbSc2.txt","w")
print 'starting...'
print >> file, 'starting...'
site2 = "https://www.oddsmonkey.com/Tools/Oddsmatcher.aspx"
driver = webdriver.Firefox()
print 'grabbing site'
print >> file, 'grabbing site'
driver.get(site2)
driver.implicitly_wait(2)
user = driver.find_element_by_id("dnn_ctr433_Login_Login_DNN_txtUsername")
password = driver.find_element_by_id("dnn_ctr433_Login_Login_DNN_txtPassword")
user.send_keys('myusername')
password.send_keys('mypassword')
submit = driver.find_element_by_id("dnn_ctr433_Login_Login_DNN_cmdLogin")
submit.click()
time.sleep(3)
close = driver.find_element_by_xpath("//button[@class='rltbActionButton rltbCloseButton']")
close.click()
driver.implicitly_wait(10)
try:
print 'attempting to find the table'
print >> file, 'attempting to find the table'
table = driver.find_element_by_xpath("//table[@id='RAD_SPLIITER_dnn_ctr956_View_RadSpliter1']")
print 'successfully found table'
print >> file, 'attempting table find'
print table.text
print >> file, table.text
except:
print 'failed to find table'
print >> file, 'failed to find table'
try:
print 'attempting to find row'
print >> file, 'attempting to find row'
row = table.find_element_by_xpath('tr')
print 'successfully found row'
print >> file, 'successfully found row'
print row.text
print >> file, row.text
except:
print 'failed to find row'
print >> file, 'failed to find row'
driver.close()
system("pause")
main()
我一直找不到 table,我不确定为什么,因为它存在于网站上,如其源代码所示:
<table id="RAD_SPLITTER_dnn_ctr956_View_RadSplitter1" class="RadSplitter RadSplitter_Telerik" style="width:1px;height:1px;border-left-width:1px;border-top-width:1px;">
确保你没有在防火墙后面。我遇到了同样的问题,在我 运行 一个 print(driver.page_source) 行之后,我意识到我的驱动程序读取的源与我通常在常规浏览器上读取的源不同。如果您确定您引用的是正确的 xpath,那么值得一试,看看您的驱动程序实际从中提取了什么。
(从一个新手到另一个 :))
我一直在尝试对 java 网站进行网络抓取(对我来说是新网站),但每次我尝试从其主要 table 读取数据时,它都会失败。我知道我要搜索的元素在网站上,所以我不知道是什么导致找不到该元素。我可以搜索其他 ares,但由于某种原因我无法读取 table 数据(没有具有相同 class 名称的元素,但我已将该站点包含在下面的代码中)。有人可以帮助我了解我可能遗漏了什么吗?
注意:该站点需要 username/password 组合,我登录没问题。
我的代码如下:
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import sys, os, requests
from os import system
def main():
file = open("wbSc2.txt","w")
print 'starting...'
print >> file, 'starting...'
site2 = "https://www.oddsmonkey.com/Tools/Oddsmatcher.aspx"
driver = webdriver.Firefox()
print 'grabbing site'
print >> file, 'grabbing site'
driver.get(site2)
driver.implicitly_wait(2)
user = driver.find_element_by_id("dnn_ctr433_Login_Login_DNN_txtUsername")
password = driver.find_element_by_id("dnn_ctr433_Login_Login_DNN_txtPassword")
user.send_keys('myusername')
password.send_keys('mypassword')
submit = driver.find_element_by_id("dnn_ctr433_Login_Login_DNN_cmdLogin")
submit.click()
time.sleep(3)
close = driver.find_element_by_xpath("//button[@class='rltbActionButton rltbCloseButton']")
close.click()
driver.implicitly_wait(10)
try:
print 'attempting to find the table'
print >> file, 'attempting to find the table'
table = driver.find_element_by_xpath("//table[@id='RAD_SPLIITER_dnn_ctr956_View_RadSpliter1']")
print 'successfully found table'
print >> file, 'attempting table find'
print table.text
print >> file, table.text
except:
print 'failed to find table'
print >> file, 'failed to find table'
try:
print 'attempting to find row'
print >> file, 'attempting to find row'
row = table.find_element_by_xpath('tr')
print 'successfully found row'
print >> file, 'successfully found row'
print row.text
print >> file, row.text
except:
print 'failed to find row'
print >> file, 'failed to find row'
driver.close()
system("pause")
main()
我一直找不到 table,我不确定为什么,因为它存在于网站上,如其源代码所示:
<table id="RAD_SPLITTER_dnn_ctr956_View_RadSplitter1" class="RadSplitter RadSplitter_Telerik" style="width:1px;height:1px;border-left-width:1px;border-top-width:1px;">
确保你没有在防火墙后面。我遇到了同样的问题,在我 运行 一个 print(driver.page_source) 行之后,我意识到我的驱动程序读取的源与我通常在常规浏览器上读取的源不同。如果您确定您引用的是正确的 xpath,那么值得一试,看看您的驱动程序实际从中提取了什么。 (从一个新手到另一个 :))