亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Python 中使用 BeautifulSoup、Selenium 解析表

在 Python 中使用 BeautifulSoup、Selenium 解析表

holdtom 2023-08-08 15:01:06
https://rocketreach.co/horizon -blue-cross-blue-shield-of-new-jersey-email-format_b5c604a3f42e0c54 這是我試圖從中獲取信息的鏈接。我需要提取表中的格式“first '_' last”“first_initial last”等等。如果不是全部,那么至少是最上面的格式。這是我到目前為止所擁有的:def search_on_google(key_word, driver):    driver.get("https://www.google.com/")    searchBoard = driver.find_element_by_name('q')    searchBoard.send_keys(key_word + " Rocketreach.co")    searchBoard.send_keys(Keys.TAB)    searchBoard.send_keys(Keys.ENTER)    driver.find_element_by_tag_name("cite").click()    soup = BeautifulSoup(driver.page_source, 'html.parser')    for link in soup.find_all('meta'):        content = link.get('content')        print(content)編輯:    for i in range(1):    driver.find_element_by_tag_name("cite").click()    soup = BeautifulSoup(driver.page_source, 'html.parser')    WebDriverWait(driver, 10).until(EC.presence_of_element_located(        (By.XPATH, "//table/tbody/tr[1]/td[1][not(contains(text(), 'Lorem ipsum...'))]")))    table_id = driver.find_element(By.TAG_NAME, "tbody")    rows = table_id.find_elements(By.TAG_NAME, "tr")    for row in rows:        tds = row.find_elements(By.TAG_NAME, "td")        top_format.append(tds[0].text)        domain.append(tds[1].text)        print(top_format)        print(domain)        break    return top_format
查看完整描述

1 回答

?
不負相思意

TA貢獻1777條經驗 獲得超10個贊

此頁面上只有一張表格可以打印所有信息,您只需執行以下操作即可打印所有信息。它也不在任何 iframe 中。


WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//table/tbody/tr[1]/td[1][not(contains(text(), 'Lorem ipsum...'))]")))

table_id = driver.find_element(By.TAG_NAME, "tbody")

rows = table_id.find_elements(By.TAG_NAME, "tr")

for row in rows:

    tds = row.find_elements(By.TAG_NAME, "td")

    for td in tds:

       one_urls.append(td.text)

print(one_urls)

您可以在打印之前進行檢查,也可以進行范圍檢查。


if tds[0] =='':

我還建議在查找表格之前稍等一下,因為您在獲取表格之前單擊并加載了新頁面。


table_id= WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "tbody")))

導入這些


from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait 

from selenium.webdriver.support import expected_conditions as EC


查看完整回答
反對 回復 2023-08-08
  • 1 回答
  • 0 關注
  • 213 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號