制作一个字典列表,其中键值对是 Web 元素的内部文本,使用 python 中的 selenium 抓取

make a list of dictionaries where key-value pairs are web elements' inner text, scraped using selenium in python

我在 Python 中使用 Selenium 库进行网络抓取。 “链接”是公寓(出租)的链接列表。我需要遍历并抓取有关每间公寓的关键信息,所以最后,我有一个字典列表,如下所示:

key_data = [{'Property type': 'Wohnung', 'room': '3', 'Floor': '1. Stock', 'Living space': '57 m²', 'Year of construction': 'not available'}]. 

无法想出一个 pythonic 的简短方法来做到这一点。我的代码:

key_data = []
for link in links:
    url = link
    driver.get(url)
    hdrs = driver.find_elements_by_class_name("css-cyiock.excbu0j2")#list of web elements
    undrhdrs = driver.find_elements_by_class_name("css-1ush3w6.excbu0j2")#list of emelements

已找到解决方案,在此发布以供遇到相同问题的任何人使用。请随时提出更好的解决方案。

key_data = []
for link in links:
    url = link
    driver.get(url)
    hdrs = driver.find_elements_by_class_name("css-cyiock.excbu0j2")
    undrhdrs = driver.find_elements_by_class_name("css-1ush3w6.excbu0j2")
    keyd_dict = {k.get_attribute("innerText"): v.get_attribute("innerText") for k, v in zip(hdrs, undrhdrs)}
    key_data.append(keyd_dict)