[英]Grabbing <title> tag with lxml's iterparse
我在HTML上使用lxml的iterparse
了問題。 我正在嘗試獲取<title>
的文本,但這個簡單的函數不適用於完整的網頁:
def get_title(str):
titleIter = etree.iterparse(StringIO(str), tag="title")
try:
for event, element in titleIter:
return element.text
# print "Script goes here when it doesn't work"
except etree.XMLSyntaxError:
return None
這個函數在“ <title>test</title>
”之類的簡單輸入上工作正常,但是當我給它一個完整的頁面時,它無法提取標題。
更新:這是我正在使用的HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" lang="it" xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="icon" href="http://www.tricommerce.it/tricommerce.ico" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Tricommerce - Informazioni sulla privacy</title>
<meta name="description" content="Info sulla privacy" />
<meta name="keywords" content="Accessori notebook Alimentatori Case Cavi e replicatori Controllo ventole Lettori e masterizzatori Modding Pannelli & display Dissipatori Tastiere e mouse Ventole Griglie e filtri Hardware Accessori vari Box esterni Casse e cuffie Sistemi a liquido Paste termiche vendita modding thermaltake vantec vantecusa sunmbeam sunbeamtech overclock thermalright xmod aerocool arctic cooling arctic silver zalman colorsit colors-it sharkoon mitron acmecom Info sulla privacy" />
<meta name="robots" content="index, follow" />
<link rel="stylesheet" href="http://www.tricommerce.it/css/tricommerce.css" />
<link rel="stylesheet" href="css/static.css" />
<script type="text/javascript" src="http://www.tricommerce.it/javascript/vertical_scroll.js"></script>
<script type="text/javascript">
//<![CDATA[
function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
//]]>
</script>
<link rel="stylesheet" type="text/css" href="http://www.tricommerce.it/css/chromestyle.css" />
<script type="text/javascript" src="http://www.tricommerce.it/javascript/chrome.js">
/***********************************************
* AnyLink CSS Menu script- ? Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/
</script>
</head>
</html>
另外,快速說明我為什么使用iterparse--這是因為我不想加載整個DOM只是為了在文檔的早期獲得單個標記。
您可能希望發布至少部分您實際嘗試解析的數據。 沒有這些信息,這是一個猜測。 如果<html>
元素定義了默認的XML命名空間,則在查找元素時需要使用它。 例如,看看這個簡單的文檔:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/MarkUp/SCHEMA/xhtml11.xsd"
xml:lang="en">
<head>
<title>Document Title</title>
</head>
<body>
</body>
</html>
鑒於此輸入,以下將不返回任何結果:
>>> doc = etree.parse(open('foo.html'))
>>> doc.xpath('//title')
[]
這失敗是因為我們在沒有指定命名空間的情況下尋找<title>
元素...並且沒有命名空間,解析器不會找到匹配項(因為foo:title
與bar:title
不同,假設foo:
和bar:
是定義的XML命名空間)。
您可以使用ElementTree接口顯式使用名稱空間,如下所示:
>>> doc.xpath('//html:title',
... namespaces={'html': 'http://www.w3.org/1999/xhtml'})
[<Element {http://www.w3.org/1999/xhtml}title at 0x1087910>]
這是我們的比賽。
您也可以將名稱空間前綴傳遞給iterparse的tag
參數:
>>> titleIter = etree.iterparse(StringIO(str),
... tag='{http://www.w3.org/1999/xhtml}title')
>>> list(titleIter)
[(u'end', <Element {http://www.w3.org/1999/xhtml}title at 0x7fddb7c4b8c0>)]
如果這不能解決您的問題,請發布一些示例輸入,我們將從那里開始工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.