简体   繁体   English

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

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

I do web scraping using Selenium library in Python .我在Python 中使用Selenium库进行网页抓取。 "Links" is a list of links for apartments(for rent). “链接”是公寓(出租)的链接列表。 I need to iterate through and scrape key info about each apartment, so in the end, I have a list of dictionaries that looks like this:我需要遍历并抓取每个公寓的关键信息,所以最后,我有一个字典列表,如下所示:

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

Cannot come up with a pythonic, short way to do it.无法想出一个pythonic,简短的方法来做到这一点。 My code:我的代码:

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

Found a solution, posting here for anyone who has the same issue.找到了一个解决方案,在这里发布给任何有同样问题的人。 Please feel free to suggest a better solution.请随时提出更好的解决方案。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM