简体   繁体   English

Python-Selenium table-scraper 只返回第一行

[英]Python-Selenium table-scraper only returns first row

I'm trying to extract information from a table with selenium.我正在尝试从带有硒的表中提取信息。

I got the rows:我得到了行:

rows = driver.find_elements_by_xpath('//tbody/tr')

And I'm trying to get two specific cells within the row:我试图在行中获取两个特定的单元格:

for r in rows:
    diccionario["property1"] = driver.find_element_by_xpath(xpath).text
    diccionario["property2"] = driver.find_element_by_xpath(xpath).text

with open("bbdd.json", "a", encoding="utf-8") as bd:
    json.dump(diccionario, bd, ensure_ascii=False, indent=4)

However, it will only return the information from the first row (repeated as many times as the number of rows)但是,它只会返回第一行的信息(重复次数与行数相同)

Is there a way to "force" the code to find the elements within the row we're currently iterating in the for loop?有没有办法“强制”代码查找我们当前在 for 循环中迭代的行中的元素?

Code代码

All the trs (rows) code look the same.所有的 trs(行)代码看起来都一样。

The tds/cells that I need are the first two (the classes look the same for every td in the different rows as well).我需要的 tds/cells 是前两个(不同行中的每个 td 的类看起来都相同)。

在此处输入图片说明

I was focused on trying to access the cell itself.我专注于尝试访问单元格本身。 However, I changed my mind and tried to reach the content from the "webelement parent" (the row itself).但是,我改变了主意并尝试从“webelement 父级”(行本身)访问内容。

The row's text property had the information that I needed.该行的 text 属性包含我需要的信息。

The only problem was that I had to parse some text (not that hard to do).唯一的问题是我必须解析一些文本(并不难)。 So in the end I didn't need to access the different tds.所以最后我不需要访问不同的 tds。

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

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