使用python 3和Selenium来刮取动态生成的表?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (63)

我是Python的新手,并试图抓取动态生成的表。我已经足够打开页面,输入搜索,并显示结果表。我在抓取结果时遇到问题,我注意到结果的具体文本不是HTML的一部分。到目前为止,这是我的代码,感谢任何和所有的帮助。

## module importation
import os, requests, bs4, openpyxl, webbrowser, lxml, html5lib, re
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

print('Type in the FIRST NAME of the individual.')
#I've been using [Mike] here.
firstName = input()
print('Thanks. Now type in the individual\'s LAST NAME.')
#I've been using [Jones] here.
lastName = input()

browser = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')
#BoP inmate locator

#Goes to BoP website
browser.get('https://www.bop.gov/inmateloc/')
res = requests.get('https://www.bop.gov/inmateloc/')

#Clicks Search by name option (just in case)
searchByNameButton = browser.find_element_by_css_selector("#ui-id-1")
searchByNameButton.click() # clicks the Search by Name Button

#enters first name
bopSearchFirstNameElem = 
browser.find_element_by_css_selector('#inmNameFirst')
bopSearchFirstNameElem.send_keys(firstName)

#enters last name
bopSearchLastNameElem = 
browser.find_element_by_css_selector('#inmNameLast')
bopSearchLastNameElem.send_keys(lastName)

# Clicks search
searchSubmitButton = 
browser.find_element_by_css_selector('#searchNameButton')
searchSubmitButton.click() # clicks the Search Button on the BoP page

# Scrape table results
bopResultsPage = bs4.BeautifulSoup(res.text, 'html.parser')
提问于
用户回答回答于

这将完美地工作:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import WebDriverWait

firstName = input('Insert your first name: ')
lastName = input('Insert your last name: ')

browser = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')
browser.get('https://www.bop.gov/inmateloc/')
browser.implicitly_wait(2)
browser.find_element_by_css_selector("#ui-id-1").click()
browser.find_element_by_css_selector('#inmNameFirst').send_keys(firstName)
browser.find_element_by_css_selector('#inmNameLast').send_keys(lastName)
browser.find_element_by_css_selector('#searchNameButton').click()
WebDriverWait(browser, 5).until(expected_conditions.text_to_be_present_in_element((By.XPATH, '//*[@id="nameBriefTd"]'), 'Results for search'))

for row in browser.find_elements_by_xpath('//*[@id="inmateTable"]/tbody/tr'):
    for cell in row.find_elements_by_xpath('td'):
        print(cell.text)
    print()

browser.close()

扫码关注云+社区

领取腾讯云代金券

年度创作总结 领取年终奖励