【发布时间】:2018-06-11 08:23:03
【问题描述】:
我正在尝试使用 beautifulsoup 和 re 来获取 URL 列表,但我想抑制其中一个结果,但我不知道该怎么做。
这段代码为我提供了 29 个(共 35 个)网址:
issue_index = soup.find_all('a', href=re.compile('past'))
这太多了。
其中一个 URL 包含 target="_blank",我想从其他 URL 中排除此 URL。
但是,我不知道该怎么做。
这与我想要的完全相反,只返回我想要丢弃的 URL。
issue_index = soup.find_all('a', href=re.compile('past'), target="_blank")
此代码删除了错误的 URL(带有 target 属性),但它没有使用正则表达式过滤列表。
def remove(a):
return a.has_attr('href') and not a.has_attr('target')
issue_index = soup.find_all(remove)
这太疯狂了。
【问题讨论】:
-
真的,毫无头绪的投反对票。
标签: python regex python-2.7 beautifulsoup