Python 对于具有漂亮汤投掷错误的循环

Python 对于具有漂亮汤投掷错误的循环,python,beautifulsoup,urllib,Python,Beautifulsoup,Urllib,我创建了这个for循环来查找以“td_threadtitle”开头的td项: for item in posts: hello = item.find("td", {"id": lambda L: L and L.startswith('td_threadtitle')}) print(hello) 但我得到了这个错误: hello = item.find("td", {"id": lambda L: L and L.startswith('td_threadtitle'

我创建了这个for循环来查找以“td_threadtitle”开头的td项:

for item in posts:
    hello = item.find("td", {"id": lambda L: L and L.startswith('td_threadtitle')})
    print(hello)
但我得到了这个错误:

hello = item.find("td", {"id": lambda L: L and    L.startswith('td_threadtitle')})
TypeError: slice indices must be integers or None or have an __index__ method
当我将变量hello更改为以下内容时:

hello=item.findtd,它工作得非常好。为什么在我尝试指定id时它会抛出该错误

编辑: 这就是我创建帖子的方式:

tableWithPosts = soup.find("body").find("div", attrs = {"align": "center"}).find("div", {"class" : "page"}).find("div", attrs = {"style" : "padding:0px 0px 0px 0px"}).find("center").find("form").find("table", {"id": "threadslist"})
posts = tableWithPosts.find("tbody", {"id": "threadbits_forum_75"}
以下是部分帖子:

</a>
     )
    </span>
   </div>
   <div class="smallfont">
    <span onclick="window.open('member.php?s=625e629b088a68126ca2d867c056b363&amp;u=206824', '_self')" style="cursor:pointer">
     thelavenhagen
    </span>
   </div>
  </td>
  <td class="alt2" title="Replies: 11, Views: 1,471">
   <div class="smallfont" style="text-align:right; white-space:nowrap">
    Thu, May-25-2017
    <span class="time">
     05:06:46 AM
    </span>
    <br/>
    by
    <a href="member.php?s=625e629b088a68126ca2d867c056b363&amp;find=lastposter&amp;t=581132" rel="nofollow">
     westopher
    </a>
    <a href="showthread.php?s=625e629b088a68126ca2d867c056b363&amp;p=1067660274#post1067660274">
     <img alt="Go to last post" border="0" class="inlineimg" src="images/buttons/lastpost.gif"/>
    </a>
   </div>
  </td>
  <td align="center" class="alt1">
   <a href="misc.php?do=whoposted&amp;t=581132" onclick="who(581132); return false;">
    11
   </a>
  </td>
  <td align="center" class="alt2">
   1,471
  </td>
 </tr>
 <tr>
  <td class="alt1" id="td_threadstatusicon_558556">
   <img alt="" border="" id="thread_statusicon_558556" src="images/statusicon/thread_hot.gif"/>
  </td>
  <td class="alt2">
   <img alt="" border="0" src="images/icons/icon1.gif"/>
  </td>
  <td class="alt1" id="td_threadtitle_558556" title="1996 E36 M3 Lux Dakar Yellow, 87,800 miles, special order without sunroof. Second owner, owned...">
   <div>
    <span style="float:right">
     <a href="#" onclick="attachments(558556); return false">
      <img alt="4 Attachment(s)" border="0" class="inlineimg" src="images/misc/paperclip.gif"/>
     </a>
    </span>
    <span style="color: blue">
     <b>
      <u>
       FS:
      </u>
     </b>
    </span>
    <a href="showthread.php?s=625e629b088a68126ca2d867c056b363&amp;t=558556" id="thread_title_558556">
     1996 E36 M3 - Dakar Lux Slicktop
    </a>
    <span class="smallfont" style="white-space:nowrap">
     (
     <img alt="Multi-page thread" border="0" class="inlineimg" src="images/misc/multipage.gif"/>
     <a href="showthread.php?s=625e629b088a68126ca2d867c056b363&amp;t=558556">
      1
     </a>
     <a href="showthread.php?s=625e629b088a68126ca2d867c056b363&amp;t=558556&amp;page=2">
      2
     </a>
     <a href="showthread.php?s=625e629b088a68126ca2d867c056b363&amp;t=558556&amp;page=3">
      3
     </a>
     )
    </span>
   </div>
   <div class="smallfont">
    <span onclick="window.open('member.php?s=625e629b088a68126ca2d867c056b363&amp;u=95931', '_self')" style="cursor:pointer">
     yellowbee
    </span>
   </div>
  </td>
  <td class="alt2" title="Replies: 23, Views: 5,147">
   <div class="smallfont" style="text-align:right; white-space:nowrap">
    Thu, May-25-2017
    <span class="time">
     04:04:07 AM
    </span>
    <br/>
    by
    <a href="member.php?s=625e629b088a68126ca2d867c056b363&amp;find=lastposter&amp;t=558556" rel="nofollow">
     mbausa
    </a>
    <a href="showthread.php?s=625e629b088a68126ca2d867c056b363&amp;p=1067660244#post1067660244">
     <img alt="Go to last post" border="0" class="inlineimg" src="images/buttons/lastpost.gif"/>
    </a>
   </div>
  </td>
  <td align="center" class="alt1">
   <a href="misc.php?do=whoposted&amp;t=558556" onclick="who(558556); return false;">
    23
   </a>
  </td>
  <td align="center" class="alt2">
   5,147
  </td>
 </tr>
 <tr>
  <td class="alt1" id="td_threadstatusicon_580693">
   <img alt="" border="" id="thread_statusicon_580693" src="images/statusicon/thread_hot.gif"/>
  </td>
  <td class="alt2">
   <img alt="" border="0" src="images/icons/icon1.gif"/>
  </td>
  <td class="alt1" id="td_threadtitle_580693" title="Selling my wife's car. We have owned her for two years and have put over 20k hassle free miles on...">
   <div>
    <span style="color: blue">
     <b>
      <u>
       FS:
      </u>
     </b>
    </span>
    <a href="showthread.php?s=625e629b088a68126ca2d867c056b363&amp;t=580693" id="thread_title_580693">
     2011 BMW 740Li Alpine White M Package Dakota Brown Interior Weather-tech Mats
    </a>
   </div>
   <div class="smallfont">
    <span onclick="window.open('member.php?s=625e629b088a68126ca2d867c056b363&amp;u=128641', '_self')" style="cursor:pointer">
     911-AL
    </span>
   </div>
  </td>

请删除for循环,然后尝试以下操作:

hello = posts.find_all("td", {"id": lambda L: L and L.startswith('td_threadtitle')})
hello
它将查找以“td_threadtitle”开头的所有td项目


hello将是一个列表,其中包含所有以“td_threadtitle”开头的TDObject,您仍然可以访问它们的div。

您可能正在使用str项的find方法。str.findsub[,start[,end]]要求start和end为整数或无。你的意思是使用BeautifulSoup的查找方法吗?是的,我正在尝试使用BeautifulSoup的查找方法。为什么我不能在“项目”上使用它?项目的帖子类型和内容类型是什么?项目是一个简单的字符串或其他东西?请看我的编辑,帖子是用美丽的汤找到的function@ceza你有什么想法吗?但如果我需要访问每个歌手的div怎么办?