【发布时间】:2016-08-31 18:17:35
【问题描述】:
使用 BeautifulSoup,我想在它们的 href 字符串中只返回包含“Company”而不是“Sector”的“a”标签。有没有办法在 re.compile() 中使用正则表达式来只返回公司而不是部门?
代码:
soup = soup.findAll('tr')[5].findAll('a')
print(soup)
输出
[<a class="example" href="../ref/index.htm">Example</a>,
<a href="?Company=FB">Facebook</a>,
<a href="?Company=XOM">Exxon</a>,
<a href="?Sector=5">Technology</a>,
<a href="?Sector=3">Oil & Gas</a>]
使用这个方法:
import re
soup.findAll('a', re.compile("Company"))
返回:
AttributeError: 'ResultSet' object has no attribute 'findAll'
但我希望它返回(没有扇区):
[<a href="?Company=FB">Facebook</a>,
<a href="?Company=XOM">Exxon</a>]
使用:
- Urllib.request 版本:3.5
- BeautifulSoup 版本:4.4.1
- 熊猫版本:0.17.1
- Python 3
【问题讨论】:
标签: python web-scraping tags beautifulsoup recompile