筛选url列表并获取“类型为'NoneType'的参数不可编辑”

2024-06-30 07:46:36 发布

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

我正在使用Selenium获取URL列表。在我想对它进行筛选之后,获取具有特定字符串的字符串,但我正在获取并出错:argument of type 'NoneType' is not iterable

Python代码:

enlaces = []
for curso in range(2):
   tema = driver.find_elements_by_class_name("user-status-off")
   tema[i].click()
   sleep(2)

   enlace = driver.find_elements_by_tag_name("a")
   for links in enlace:
      enlaces.append(links.get_attribute("href"))

   i += 1

filtered_links = [k for k in enlaces if "/o/" in k]

如果我做了print(enlaces)I,我会得到正确的结果:

https://xxxxxxxxx/portlet-view?page_num=42552006
https://xxxxxxxxx/o/43277208

我只想要那些包含/o/的,因此我使用[k for k in enlaces if "/o/" in k] 。我做错了什么?用于获取argument of type 'NoneType' is not iterable

谢谢

p.D.: 我也试过list(filter(lambda k: '/o/' in k, enlaces))得到同样的结果


Tags: of字符串inforistypedrivernot
1条回答
网友
1楼 · 发布于 2024-06-30 07:46:36

这将解决问题

filtered_links = [k for k in enlaces if k and "/o/" in k]

说明: 根据@G.Anderson和@Kenticent的调查结果,显示了错误,因为在某些情况下k值为零。如果执行k中的k和“/o/”,如果k为无,则不检查第二部分

相关问题 更多 >