Eri*_*Kim 3 python beautifulsoup
我在网站和stackoverflow上看了很多例子,但我找不到我的问题的通用解决方案.我正在处理一个非常混乱的网站,我想抓一些数据.标记看起来像这样:
...
<body>
...
<table>
<tbody>
<tr>
...
</tr>
<tr>
<td>
...
</td>
<td>
<table>
<tr>
...
</tr>
<tr>
<td>
<a href="...">Some link</a>
<a href="...">Some link</a>
<a href="...">Some link</a>
</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
</body>
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是没有任何元素具有我可以选择缩小范围的属性.里面每个"......"有可能是类似的标记,如更多<a>的<table>和诸如此类的东西.
我知道这table tr table tr td a是我需要的链接所独有的,但BeautifulSoup将如何抓住这些链接?我不确定如何在不做一堆单独的代码行的情况下抓取嵌套标签.
有帮助吗?
Pav*_*sov 11
soup.select('table tr table tr td a')
Run Code Online (Sandbox Code Playgroud)
In [32]: bs4.BeautifulSoup(urllib.urlopen('http://google.com/?hl=en').read()).select('#footer a')
Out[32]:
[<a href="/intl/en/ads/">Advertising Programs</a>,
<a href="/services/">Business Solutions</a>,
<a href="https://plus.google.com/116899029375914044550" rel="publisher">+Google</a>,
<a href="/intl/en/about.html">About Google</a>,
<a href="http://www.google.com/setprefdomain?prefdom=RU&prev=http://www.google.ru/&sig=0_3F2sRGWVktTCOFLA955Vr-AWlHo%3D">Google.ru</a>,
<a href="/intl/en/policies/">Privacy & Terms</a>]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4267 次 |
| 最近记录: |