我想解析HTML使用BeautifulSoup嘗試和提取網頁標題。有時候,由於網站寫得很糟糕,如Bad End標籤,這是行不通的。當這不工作,我去手動正則表達式Python的正則表達式找不到子字符串,但它應該
我有文字
<html xmlns="http://www.w3.org/1999/xhtml"\n xmlns:og="http://ogp.me/ns#"\n xmlns:fb="https://www.facebook.com/2008/fbml">\n<head>\n <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>\n <title>\n [email protected] prepping questions for the Cheney intvw. @CNNSitRoom today. 5p. \n </title>\n <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />...
,我試圖抓住<title>
和</title>
標籤之間的值。它應該相當簡單,但它不起作用。這是我的Python代碼。
result = re.search('\<title\>(.+?)\</title\>', html)
if result is not None:
title = result.group(0)
這對任何原因都不適用於本文。它將result.group()返回爲None,或者我得到一個AttributeError。 AttributeError:'NoneType'對象沒有屬性'groups'
我C &把這段文字轉換成在線python正則表達式的開發者並嘗試了所有的選項(re.match,re.findall,re.search)和他們在那裏工作,但無論在我的腳本中的任何原因,它都無法找到這些標籤之間的任何東西。即使嘗試其他的正則表達式,如
<title>(.*?)</title>
等
爲什麼're.DOTALL'標誌?你甚至不使用'.'。 – ohaal
@ohaal:對!非常感謝。 – user278064