![](/img/trans.png)
[英]Returns a NoneType value while using find function in Beautiful Soup
[英]Using the find function to determine if a value is found in a list with Beautiful Soup
我正在从页面中抓取数据并尝试使用find
和 Beautiful Soup 来确定某个值是否在列表中。 因此,如果在页面上找到“4:30”,则在这种情况下执行其他操作:
myList = ['4:28', '4:29', '4:30']
if str(soup).find(myList) == -1:
# continue with the script,
continue
else
# do something else
我也试过:
if str(soup).find('4:28') == -1 or str(soup).find('4:29') == -1 or str(soup).find('4:30') == -1:
# continue with the script,
continue
else
# do something else
两者似乎都不起作用,但我更喜欢一个列表,因为我可能需要找到 30-60 个值。
它可能不是性能最高的解决方案,但如果您的字符串不是那么大:
myList = ['4:28', '4:29', '4:30']
string1 = "34:28, 4:29, 4:30"
string2 = "other test 5:3"
if any(pattern in string1 for pattern in myList):
print("found pattern in string 1")
else:
print("found no pattern in string 1")
if any(pattern in string2 for pattern in myList):
print("found pattern in string 2")
else:
print("found no pattern in string 2")
但是,这个问题并没有真正与 BeautifulSoup 相关联,而只是与 Python 中的字符串匹配相关联。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.