我正在尝试从 .jsp 页面中抓取表格(详情如下)。表格仅在输入数据后加载(火车号和旅程站)
对于您的试验,列车号可以是56913,旅程站可以是SBC(输入数据后,这将自动更改为“KSR Bengaluru”。
使用下面的脚本,我能够生成表格,但是,我无法提取它(在空列表中打印结果)。我需要得到完整的桌子。任何人都可以帮助知道如何提取表格吗?
我对网络抓取非常陌生。因此,如果犯了一些基本错误,请朝正确的方向轻轻推动我。
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.firefox.options import Options
from selenium.webdriver import Firefox
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from bs4 import BeautifulSoup
import soupsieve as sv
import requests
# Activate the following line if you do not want to see the Firefox window.
# Better deactivate it for debugging.
# os.environ['MOZ_HEADLESS'] = '1'
url = 'https://enquiry.indianrail.gov.in/ntes/trainOnMapBh.jsp'
opts = Options()
driver = Firefox(firefox_binary=r"C:\Program Files (x86)\Mozilla Firefox\firefox.exe", options=opts)
driver.get(url)
WebDriverWait(driver, 20)
train_field = driver.find_element_by_id("trnSrchTxt")
train_field.send_keys("56913")
time.sleep(2)
actions = ActionChains(driver)
actions.send_keys('SBC',Keys.ENTER)
actions.perform()
WebDriverWait(driver, 1)
result_table = driver.find_elements_by_id("mapTrnSch")
print(result_table)
更新 除了来自@MadRay 的答案之外,以下代码还获取了数据(不确定它有多健壮)。
import os
import time
from bs4 import BeautifulSoup
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver import Firefox
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import re
os.environ['MOZ_HEADLESS'] = '1'
opts = Options()
driver = Firefox(firefox_binary=r"C:\Program Files (x86)\Mozilla Firefox\firefox.exe", options=opts)
driver.get('https://enquiry.indianrail.gov.in/ntes/trainOnMapBh.jsp')
WebDriverWait(driver, 20)
繁花不似锦
相关分类