Python 使用bs4进行webscraping验证
不知道为什么,但今天的代码并没有像预期的那样工作(我曾经在其他时候对不同的html使用过相同的代码,并且工作得非常出色)。 我试图找出文本“Anular”是否在HTML代码中。但是当文本在那里可见时,总是以“否”的形式出现 代码是:Python 使用bs4进行webscraping验证,python,beautifulsoup,Python,Beautifulsoup,不知道为什么,但今天的代码并没有像预期的那样工作(我曾经在其他时候对不同的html使用过相同的代码,并且工作得非常出色)。 我试图找出文本“Anular”是否在HTML代码中。但是当文本在那里可见时,总是以“否”的形式出现 代码是: from bs4 import BeautifulSoup import re html = browser.page_source soup = BeautifulSoup(html, "html.parser") if s
from bs4 import BeautifulSoup
import re
html = browser.page_source
soup = BeautifulSoup(html, "html.parser")
if soup.findAll(text = re.compile('Anular')):
registo2 = "Yes"
else:
registo2 = "No"
Html是(它的一部分):
马尔卡达
在您提供的html中,您要查找的字符串包含在标记的“value”属性中。
所以你的代码应该是:
if soup.find_all(value=re.compile('Anular')):
registo2 = "Yes"
else:
registo2 = "No"
或者,如果要在文本或属性中检查字符串,可以在中使用“全部查找”
def f(tag):
return 'Anular' in tag.get('value', '') or 'Anular' in (tag.string or '')
registo2 = "Yes" if soup.find_all(f) else "No"
您的导入是在声明解析器之后进行的。从空闲复制粘贴错误。我会马上纠正的。非常感谢你。我现在明白了。
def f(tag):
return 'Anular' in tag.get('value', '') or 'Anular' in (tag.string or '')
registo2 = "Yes" if soup.find_all(f) else "No"