使用Python在族搜索记录中搜索数据

2024-06-15 00:30:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在familysearch.org中删除以下记录表。我将ChromeWebDriver与Python一起使用,使用BeautifulSoup和Selenium

在检查我感兴趣的页面时,我想从下面的HTML中略过一些内容。注意,这只是familysearch.org表中有100个名称的一个元素部分

<span role="cell" class="td " name="name" aria-label="Name"> <dom-if style="display: none;"><template is="dom-if"></template></dom-if> <dom-if style="display: none;"><template is="dom-if"></template></dom-if> <span><sr-cell-name name="Jame Junior " url="ZS" relationship="Principal" collection-name="Index"></sr-cell-name></span> <dom-if style="display: none;"><template is="dom-if"></template></dom-if> </span>

或者,名称也会显示在这段HTML中

<a class="name" href="/ark:ZS">Jame Junior </a>

从所有这些中,我只想得到名字“Jame Junior”,我试过使用driver.find.elements_by_class_name("name"),但它什么也不打印

这是我使用的代码

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup
import pandas as pd
from getpass import getpass


username = input("Enter Username: " )
password = input("Enter Password: ")
chrome_path= r"C:\Users...chromedriver_win32\chromedriver.exe"
driver= webdriver.Chrome(chrome_path)
driver.get("https://www.familysearch.org/search/record/results?q.birthLikeDate.from=1996&q.birthLikeDate.to=1996&f.collectionId=...")

usernamet = driver.find_element_by_id("userName")
usernamet.send_keys(username)
passwordt = driver.find_element_by_id("password")
passwordt.send_keys(password)
login = driver.find_element_by_id("login")
login.submit()
driver.get("https://www.familysearch.org/search/record/results?q.birthLikeDate.from=1996&q.birthLikeDate.to=1996&f.collectionId=.....")
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CLASS_NAME, "name")))
#for tag in driver.find_elements_by_class_name("name"):
 #   print(tag.get_attribute('innerHTML'))

for tag in soup.find_all("sr-cell-name"):
    print(tag["name"])

Tags: namefromorgimportbyifdrivercell
2条回答

我希望做一些非常类似的事情,并且有半体面的python/selenium抓取经验。长话短说,FamilySearch(我相信还有许多其他网站)使用了某种涉及影子主机的技术(我不是JS或网络爱好者)。标签基本上对BS或Selenium不可见

解决方案:pyshadow https://github.com/sukgu/pyshadow

您还可能发现此链接很有帮助: How to handle elements inside Shadow DOM from Selenium

我现在已经能够成功地找到以前找不到的元素,但仍然没有找到我想要得到的东西。祝你好运

尝试访问sr-cell-name标记

Selenium

for tag in driver.find_elements_by_tag_name("sr-cell-name"):
    print(tag.get_attribute("name"))

BeautifulSoup

for tag in soup.find_all("sr-cell-name"):
    print(tag["name"])

编辑:在解析元素之前,可能需要等待元素完全出现在页面上。可以使用^{}方法执行此操作:

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


driver = webdriver.Chrome()
driver.get("...")

WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CLASS_NAME, "name")))

for tag in driver.find_elements_by_class_name("name"):
    print(tag.get_attribute('innerHTML'))

相关问题 更多 >