所以我尝试使用 python 比较 2 个列表,其中一个包含我从网站获取的 1000 个链接。另一个包含一些单词,这些单词可能包含在第一个列表的链接中。如果是这种情况,我想得到一个输出。我打印了第一个列表,它确实有效。例如,如果链接是“https://steamcdn-a.swap.gg/apps/730/icons/econ/stickers/eslkatowice2015/counterlogic.f49adabd6052a558bff3fe09f5a09e0675737936.png https://steamcdn-a.swap.gg/apps/730/icons/econ/stickers/eslkatowice2015/counterlogic.f49adabd6052a558bff3fe09f5a09e0675737936.png”并且我的列表包含单词“eslkatowice2015”,我想使用以下命令获得输出print()
功能。我的代码如下所示:
page_source = driver.page_source
soup = BeautifulSoup(page_source, 'lxml')
Bot_Stickers = soup.find_all('img', class_='csi')
for sticker in Bot_Stickers:
for i in StickerIDs:
if i in sticker:
print("found")
driver.close()
现在的问题是我没有得到输出,这是不可能的,因为如果我手动比较列表,显然第二个列表中存在第一个列表中的元素(带有链接的列表)。当尝试修复时,我总是收到 NoneType 错误。 driver.page_source 是由我用来访问站点并单击一些 javascript 内容的一些 selenium 定义的,以便能够找到所有内容。我希望它或多或少清楚我想要达到的目标
编辑:StickerIDs 变量是包含我要检查的单词的第二个列表
NoneType
错误意味着您可能是,所以检查返回的结果可能更安全find_all
for None
.
自从使用 BeautifulSoup 以来已经有一段时间了,但是如果我没记错的话,find_all
返回一个列表beautiful soup tags
匹配搜索条件,而不是 URL。您需要获得href
在检查标签是否包含关键字之前,先从标签中获取属性。
像这样的东西:
page_source = driver.page_source
soup = BeautifulSoup(page_source, 'lxml')
Bot_Stickers = soup.find_all('img', class_='csi')
if Bot_Stickers and StickersIDs:
for sticker in Bot_Stickers:
for i in StickerIDs:
if i in sticker.get("href"): # get href attribute of the img tag
print("found")
else:
print("Bot_Stickers:", Bot_Stickers)
print("StickersIDs:" StickersIDs)
driver.close()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)