简体   繁体   English

错误的美丽汤

[英]Error with Beautiful Soup

I have to remove the text in the title tag from this source: 我必须从此源中删除标题标签中的文本:

<!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 &amp; 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>

I am using this to remove the text: 我正在使用它来删除文本:

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')

This code correctly extracts the p tags however fails when trying to extract title. 此代码正确提取了p标签,但是在尝试提取标题时失败。 Thanks. 谢谢。 I have only included a part of the code, dont worry the rest of it works fine. 我只包含了一部分代码,不用担心其余的代码工作正常。

There is an error in your html code! 您的html代码有错误! You have 2 </title> endtags: 您有2个</title>结束标签:

<!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 &amp; 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> #You already have endtag of <title>

So the fixed code should look like this: 因此,固定代码应如下所示:

<!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 &amp; 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" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM