【发布时间】:2018-06-16 09:19:35
【问题描述】:
我一直在尝试对一个 java 网站进行网页抓取(对我来说是新的),但每次我尝试从其主表中读取数据时它都会失败。我知道我要搜索的元素在网站上,所以我不知道是什么导致找不到该元素。我可以搜索其他领域,但由于某种原因,我无法读取表数据(没有具有相同类名称的元素,但我已将站点包含在下面的代码中)。有人可以帮助我了解我可能缺少什么吗?
注意:该网站需要用户名/密码组合,我可以顺利登录。
我的代码如下:
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 id="RAD_SPLITTER_dnn_ctr956_View_RadSplitter1" class="RadSplitter RadSplitter_Telerik" style="width:1px;height:1px;border-left-width:1px;border-top-width:1px;">
【问题讨论】:
标签: java python-2.7 selenium web-scraping selenium-firefoxdriver