打开新的网页选项卡后无法抓取表格

时间:2020-04-16 21:40:23

标签: python python-3.x selenium beautifulsoup

我正在使用selenium程序包从一个网页转到另一个网页。之后,我想使用beautifulsoup从生成的表中抓取。

GameDescriptionActivity

但是,我得到的唯一的html是以下内容...

#Open Webpage
url = "https://www.website.com"
driver=webdriver.Chrome(executable_path=r"C:\mypathto\chromedriver.exe")
driver.get(url)

#Click Necessary Parameters
driver.find_element_by_partial_link_text('Output').click()
driver.find_element_by_xpath('//*[@id="flexOpt"]/table/tbody/tr/td[2]/input[3]').click()
driver.find_element_by_xpath('//*[@id="flexOpt"]/table/tbody/tr/td[2]/input[4]').click()
driver.find_element_by_xpath('//*[@id="repOpt"]/table[2]/tbody/tr/td[2]/input[4]').click()
time.sleep(2)

driver.find_element_by_partial_link_text('Dates').click()
driver.find_element_by_xpath('//*[@id="RangeOption"]').click()
driver.find_element_by_xpath('//*[@id="Range"]/table/tbody/tr[1]/td[2]/select/option[2]').click()
driver.find_element_by_xpath('//*[@id="Range"]/table/tbody/tr[1]/td[3]/select/option[1]').click()
driver.find_element_by_xpath('//*[@id="Range"]/table/tbody/tr[1]/td[4]/select/option[1]').click()
driver.find_element_by_xpath('//*[@id="Range"]/table/tbody/tr[2]/td[2]/select/option[2]').click()
driver.find_element_by_xpath('//*[@id="Range"]/table/tbody/tr[2]/td[3]/select/option[31]').click()
driver.find_element_by_xpath('//*[@id="Range"]/table/tbody/tr[2]/td[4]/select/option[1]').click()
time.sleep(2)

driver.find_element_by_partial_link_text('Groupings').click()
driver.find_element_by_xpath('//*[@id="availFld_DATE"]/a/img').click()
driver.find_element_by_xpath('//*[@id="availFld_LOCID"]/a/img').click()
driver.find_element_by_xpath('//*[@id="availFld_STATE"]/a/img').click()
driver.find_element_by_xpath('//*[@id="availFld_DDSO_SA"]/a/img').click()
driver.find_element_by_xpath('//*[@id="availFld_CLASS_ID"]/a/img').click()
driver.find_element_by_xpath('//*[@id="availFld_REGION"]/a/img').click()
time.sleep(2)

driver.find_element_by_partial_link_text('Run').click()
time.sleep(2)

#scrape new web page
df_url = driver.switch_to_window(driver.window_handles[1])
df_url = driver.current_url
page = requests.get(df_url).text
soup = BeautifulSoup(page, features = 'html5lib')
print(soup.prettify())

有什么理由吗?当我打开新标签时,我看到并用于刮擦。我怎样才能让python读取正在寻找的html部分?

1 个答案:

答案 0 :(得分:1)

不使用请求,而是尝试将page_source另存为文本,以解析漂亮的汤。

#scrape new web page
df_url = driver.switch_to_window(driver.window_handles[1])
df_url = driver.current_url
page = driver.page_source
soup = BeautifulSoup(page, "lxml")
print(soup.prettify())
相关问题