【问题标题】:TypeError: Type str doesn't support the buffer API # find method?TypeError: Type str 不支持缓冲区 API #find 方法?
【发布时间】:2013-11-30 16:39:23
【问题描述】:

这是我的输入:

<!DOCTYPE html>
..........
<div class="content">
      <div class="stream-item-header">
          <a class="account-group js-account-group js-action-profile js-user-profile-link js-nav" href="https://twitter.com/jimcramer" data-user-id="14216123">
    <img class="avatar js-action-profile-avatar" src="Twitter%20_%20Search%20-%20%23tsla_files/988b4c2369623b634782f4c0469ec38f_normal.jpg" alt="">
    <strong class="fullname js-action-profile-name show-popup-with-id">Jim Cramer</strong>
    <span>‏</span><span class="username js-action-profile-name"><s>@</s><b>jimcramer</b></span>
  </a>
       <small class="time">
    <a href="https://twitter.com/jimcramer/status/405348028417994752" class="tweet-timestamp js-permalink js-nav js-tooltip" title="3:51 PM - 26 Nov 13"><span class="_timestamp js-short-timestamp " data-time="1385477475" data-long-form="true">26 Nov</span></a>
</small>
      </div>
      <p class="js-tweet-text tweet-text">Love this spirited &amp; rigorous <a href="https://twitter.com/search?q=%24TSLA&amp;src=ctag" data-query-source="cashtag_click" class="twitter-cashtag pretty-link js-nav" dir="ltr"><s>$</s><b>TSLA</b></a> defense ! RT <a href="https://twitter.com/InfennonLabs" class="twitter-atreply pretty-link" dir="ltr"><s>@</s><b>InfennonLabs</b></a>: Why are these idiots selling <a href="https://twitter.com/search?q=%23tsla&amp;src=hash" data-query-source="hashtag_click" class="twitter-hashtag pretty-link js-nav" dir="ltr"><s>#</s><b><strong>tsla</strong></b></a> are they that blind? <a href="https://twitter.com/jimcramer" class="twitter-atreply pretty-link" dir="ltr"><s>@</s><b>jimcramer</b></a></p>
      <div class="stream-item-footer">
<div class="context">
      <span class="metadata with-icn">
        <i class=" badge-top"></i>Favorited 5 times</span>
</div>
...........
</html>

例如,这个“输入”在我的input 变量中。

这是我的代码:

  start_link = input.find(' <p class="js-tweet-text tweet-text" ')

如果我运行它,我会得到以下错误:

  start_link = input.find('<p class="js-tweet-text tweet-text" ')
TypeError: Type str doesn't support the buffer API

我该如何解决这个问题?

注意:我的输入变量的类型是:class 'bytes'

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    您不能使用bytes.find()bytes 对象内查找str 对象(因为它们是不同的类型,所以str 不能在bytes 内)。
    但是,您可以在其中查找字节对象。这应该有效:

    start_link = input.find(b' <p class="js-tweet-text tweet-text" ')
    

    顺便说一句,如果您正在解析 html,则应该使用 html 解析器。

    【讨论】:

      【解决方案2】:

      您还可以将来自输入的数据设为 str 对象,如下所示:

      url = "http://www.google.com"
      req = request.Request(url)
      response = request.urlopen(req)
      page = str(response.read()) # make it a str object
      print(page[page.find('id='):]) # now you don't need a b in front of your string
      

      【讨论】:

        猜你喜欢
        • 2013-03-26
        • 2015-07-26
        • 1970-01-01
        • 2011-07-25
        • 1970-01-01
        • 2014-08-09
        相关资源
        最近更新 更多