[英]Python Regex Match failed
這通過https://regex101.com/傳遞,沒有任何問題。 我有想念嗎? 整個字符串在一行中。
def get_title_and_content(html):
html = """<!DOCTYPE html> <html> <head> <title>Change delivery date with Deliv</title> </head> <body> <div class="gkms web">The delivery date can be changed up until the package is assigned to a driver.</div> </body> </html> """
title_pattern = re.compile(r'<title>(.*?)</title>(.*)')
match = title_pattern.match(html)
if match:
print('successfully extract title and answer')
return match.groups()[0].strip(), match.groups()[1].strip()
else:
print('unable to extract title or answer')
在評論摘要中:
title_pattern.search(html)
應該被用來代替title_pattern.match(html)
由於搜索功能將在提供的字符串中的任何位置進行搜索,而不僅僅是從頭開始。 match = title_pattern.findall(html)
可以類似地使用,但將返回項目列表,而不只是一個項目列表。
就像前面提到的,使用BeautifulSoup
從長遠來看會付出更多,因為正則表達式不適合搜索HTML
注釋是正確的,re.match()從頭開始搜索。 就是說,在您的正則表達式中插入。*,以便從頭開始搜索:
title_pattern = re.compile(r'.*<title>(.*?)</title>(.*)')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.