Re.Sub预期字符串或缓冲区

时间:2015-06-23 12:48:58

标签: python-2.7

我阅读并尝试thisthisthis解决方案。但我的错误没有解决。

    def read_news(self, news_url):
    try:
        self.checkRequests(news_url)
        result = self.soup.find("div", {'class', 'm-article__entry'})
        if isinstance(result, bs4.element.Tag):
            for r in result:
                re.sub("<.*?>", "", r)
            print ('result', result)
            return result.lower()
        return
    except Exception, e:
        self.insertErrorLog('thevergetech.read_news', news_url, e)

这是代码发生错误的部分。我在调试模式下运行我的代码,结果返回:

    <div class="m-article__entry">
<p>Malloy Aeronautics, a UK-based company, has been slowly and publicly developing a hoverbike over the last few years — it even used <a href="https://www.kickstarter.com/projects/1524806320/hoverbike/description" target="new">Kickstarter</a> to raise funds. But it looks like the project is now headed in a new direction, because the US Department of Defense just announced a deal with Malloy to develop the vehicle for the US Army.</p>
<p>The DoD is interested in the technology for a few reasons. For one, it's safe. The hoverbike's rotors are guarded so they won't tear into humans and other objects. It's also a cheaper option than, say, a helicopter. And it's more maneuverable in tight spaces, with options to operate it autonomously or with a human pilot.</p>
<div class="m-ad m-ad__article-body">
<div class="dfp_ad" data-cb-ad-id="Mobile article body" data-cb-dfp-id="unit=mobile_article_body" id="div-gpt-ad-mobile_article_body">
<script type="text/javascript">
      SBN.Ads.showAd("mobile_article_body");
    </script>
</div>
</div>
<p><q class="right">From Kickstarter to the US Military-industrial complex</q></p>
<p>Developers of the hoverbike <a href="http://www.reuters.com/article/2015/06/22/us-airshow-france-hoverbike-idUSKBN0P21C720150622?feedType=RSS&amp;feedName=technologyNews">told Reuters</a> that they consider it ideal for search and rescue or cargo delivery missions. It could also be used for surveillance — plans for the full-scale version include an attachable humanoid figure with a head-mounted camera.</p>
<p>The first step in the deal will be to build a functioning full-scale model, and from there the DoD will reportedly design military-grade prototypes. In the meantime, Malloy Aeronautics will continue to make scale models while developing a commercial version of the hoverbike.</p>
<hr/>
<div class="video-wrap p-scalable-video"><div class="chorus-video-embed" data-analytics-placement="entry:middle" data-chorus-video-id="57712" id="chorus-video-57712"></div></div>
<b>Verge Video</b> <i>We rode a hoverboard</i>
<ul class="m-article__sources">
<li class="source"><strong>Source</strong><a href="http://www.reuters.com/article/2015/06/22/us-airshow-france-hoverbike-idUSKBN0P21C720150622?feedType=RSS&amp;feedName=technologyNews">Reuters</a></li>
<li class="tags"><strong>Related Items</strong>
<a href="/tag/us-army">us army</a>
<a href="/tag/department-of-defense">department of defense</a>
<a href="/tag/drones">drones</a>
<a href="/tag/hoverbike">hoverbike</a>
<a href="/tag/malloy">malloy</a>
</li>
</ul>
</div>

我尝试循环就像for r in return一样,但循环2次并得到同样的错误。

2 个答案:

答案 0 :(得分:0)

我修复了我的代码。这段代码没有错误;

def read_news(self, news_url):
        try:
            self.checkRequests(news_url)
            result = self.soup.find("div", {'class', 'm-article__entry'})
            if isinstance(result, bs4.element.Tag):
                result = str(result)
                result = re.sub("<.*?>", "", result)
                return result.lower()
            return
        except Exception, e:
            self.insertErrorLog('thevergetech.read_news', news_url, e)

答案 1 :(得分:0)

看起来你有8个空间打算比4