我必須從這個源中刪除在標題標籤的文本:錯誤與美麗的湯
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" lang="en">
<head>
<title>Microsoft to acquire Nokia’s devices & services business, license Nokia’s patents and mapping services</title>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9; IE=10" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta id="ctl00_WtCampaignId" name="DCSext.wt_linkid" />
</title>
我使用這個刪除文本:
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
ourUrl = opener.open("http://www.thehindubusinessline.com/industry-and-economy/info-tech/nokia-cannot-license-brand-nokia-post-microsoft-deal/article5156470.ece").read()
soup = BeautifulSoup(ourUrl)
print soup
dem = soup.findAll('p')
hea = soup.findAll('title')
此代碼正確地提取該p但嘗試提取標題時失敗。謝謝。我只包含了部分代碼,不用擔心其餘的工作正常。
我無法重現你的問題。那個頁面上的HTML被破壞了,但BeautifulSoup 3和所有3個用於BeautifulSoup 4的解析器插件都給了我正確的輸出,並且我可以很好地提取標題。 –
您使用的是什麼版本的BeautifulSoup? 4.0系列有一些問題。另外,某些lxml + libxml2組合在某些HTML輸入方面存在問題。如果您使用的是BeautifulSoup 4,您是否安裝了lxml? –
嗯,你會得到什麼錯誤?或者你得到一個空的列表?因爲我試過你的代碼(也是這個頁面),並且得到了** no **錯誤! – JadedTuna