python - 使用 beautifulsoup 进行网页抓取 - 导航

标签 python web-scraping beautifulsoup

我正在尝试使用 beautiful soup 来抓取一个网站。我可以导航到类对象,但无法进入下一个级别来获取我想要的文本。

到目前为止我已经

soup = BeautifulSoup(urllib2.urlopen('URL...').read())

comment = soup('div', {'class' : 'PanelDarkBackground'})
print comment 

它只输出整个类(如下)。我只想提取 0-0,它位于代码的 tr>td id="event"部分

有什么建议吗...?

[<div class="PanelDarkBackground" id="Event-Basic-Info" style="margin-bottom: 10px">
<div style="height: 70px; width: 100%;">
<div style="height: 70px; width: 70px; float: left; background-color: white">
<img height="70" src="ss" width="70"/>
</div>
<div style="width: 450px; float: left; height: 70px; display: table">
<table border="0" cellpadding="0" cellspacing="0" style="font-family: tahoma; font-size:      18pt; font-weight: bold; color: white;" width="450px">

    <tr>
      <td align="center" height="70" style="font-family: tahoma; font-size: 18pt; font-weight:    bold; color: white;" valign="middle" width="197">seveal</td>
      <td align="center" id="event" style="font-family: tahoma; font-size: 18pt; font- weight: bold; color: white;" valign="middle">0-0</td>
      <td align="center" style="font-family: tahoma; font-size: 18pt; font-weight: bold; color: white;" valign="middle" width="197">seveal</td>
    </tr>
 </table>
</div>
<div style="height: 70px; width: 70px; float: right; background-color: white">
<img height="70" src="" width="70"/>
</div>
</div>
</div>]

最佳答案

直接转到td

print soup('td',{'id':'event'})

对于 td 的内容,您可以执行以下操作:

print soup('td',{'id':'event'})[0].contents[0]

关于python - 使用 beautifulsoup 进行网页抓取 - 导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14719736/

相关文章:

python - 图像处理算法设计 : how to get front images from an angled camera?

python - 如何在 BeautifulSoup 中使用元素的样式定义(例如填充、字体大小等)来抓取元素

python - 在 'href' 标签下提取 'a'

python - BeautifulSoup,findAll 之后 findAll?

python - Zip 函数未显示已抓取数据的完整列表

python - Appengine 使用 https 而不是 http

python - 加快 Pandas 数据框中字符串的整数编码

Python:keras 形状不匹配错误

python - Beautiful Soup 使用波斯字符串查找

javascript - 在Python中使用Selenium单击特定按钮并启动for循环